caola
V2EX  ›  Lua

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

  •  
  •   caola · Aug 19, 2016 · 10403 views
    This topic created in 3565 days ago, the information mentioned may be changed or developed.

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

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

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

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

    3 replies    2025-01-07 15:34:58 +08:00
    esile
        1
    esile  
       Aug 20, 2016 via iPhone
    正则
    Lexo
        2
    Lexo  
       Apr 10, 2020
    string.find("Hello world","Hello",1,true)

    第一个参数是母字符串,第二个参数是子字符串,第三个是从第几位开始匹配,第四个设为 true,就可以开启仅字符串匹配,而不是用 Lua 正则匹配,无需转义
    wionch
        3
    wionch  
       Jan 7, 2025
    用 c 语言写个模块来实现。[狗头]
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   859 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 20:20 · PVG 04:20 · LAX 13:20 · JFK 16:20
    ♥ Do have faith in what you're doing.