caola
V2EX  ›  Lua

Lua 怎么实现 php strstr() 函数的功能

By caola at 2016 年 8 月 19 日 · 10059 次点击

最近接触了下 openresty(lua),想弄点自己的功能,但遇到小问题:

local myPosition = string.find(myStr, myValue) --查找字符所在位置 string.sub(myStr, myPosition) --取出找到的字符以及之后的字符串

PS : myValue 可能是任何字符串(有可能是字母数特殊符号和中文的混合字符串), string.find 这函数没法直接查找,得要各种转义,非常蛋疼。

想问一下还能有什么 lua 能高效的实现 php strstr() 功能的方法?

3 条回复    2025-01-07 15:34:58 +08:00
esile
   1
esile  
   2016 年 8 月 20 日 via iPhone
正则
Lexo
   2
Lexo  
   2020 年 4 月 10 日
string.find("Hello world","Hello",1,true)

第一个参数是母字符串,第二个参数是子字符串,第三个是从第几位开始匹配,第四个设为 true,就可以开启仅字符串匹配,而不是用 Lua 正则匹配,无需转义
wionch
   3
wionch  
   2025 年 1 月 7 日
用 c 语言写个模块来实现。[狗头]
© 2026 V2EX · 31ms · 3.9.8.5