加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

有没有模式的Lua string.find

发布时间:2020-12-14 21:47:20 所属栏目:大数据 来源:网络整理
导读:我应用了一个函数,但看起来很糟糕. function find_without_pattern(s1,s2) for i =1,#s1-#s2+1 do local t = string.sub(s1,i,#s2+i-1) if t == s2 then return i,i+#s2-1 end endend 解决方法 string.find方法提供了一个可选的第4个参数来自行强制执行 plai
我应用了一个函数,但看起来很糟糕.

function find_without_pattern(s1,s2)
    for i =1,#s1-#s2+1 do
        local t = string.sub(s1,i,#s2+i-1)
        if t == s2 then
            return i,i+#s2-1
        end
    end
end

解决方法

string.find方法提供了一个可选的第4个参数来自行强制执行 plaintext search.

例如:

string.find("he#.*o","e#.*o",1,true)

会给你正确的结果.

引用Lua手册页:

string.find (s,pattern [,init [,plain]])

A value of true as a fourth,optional argument plain turns off the pattern matching facilities,so the function does a plain “find substring” operation,with no characters in pattern being considered magic. Note that if plain is given,then init must be given as well.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读