String Lua字符串操作模式匹配选项“|&引用;

String Lua字符串操作模式匹配选项“|&引用;,string,parsing,lua,lua-patterns,String,Parsing,Lua,Lua Patterns,有没有一种方法可以使字符串模式匹配输入字符串中的“ab | cd”,从而匹配“ab”或“cd”。我知道您使用类似于“[ab]”的模式,它将匹配“a”或“b”,但这仅适用于一个字母的内容 注意,我的实际问题要复杂得多,但本质上我只需要知道Lua的字符串操作中是否存在OR。实际上,我想在或事物的每一面都放置其他模式,等等。但是如果它与类似于“hello | world”的东西一起工作,并将“hello,world!”与“hello”和“world”两者匹配,那就太棒了 不幸的是,它们不是正则表达式,

有没有一种方法可以使字符串模式匹配输入字符串中的
“ab | cd”
,从而匹配
“ab”
“cd”
。我知道您使用类似于
“[ab]”
的模式,它将匹配
“a”
“b”
,但这仅适用于一个字母的内容

注意,我的实际问题要复杂得多,但本质上我只需要知道Lua的字符串操作中是否存在OR。实际上,我想在或事物的每一面都放置其他模式,等等。但是如果它与类似于
“hello | world”
的东西一起工作,并将
“hello,world!”
“hello”
“world”
两者匹配,那就太棒了

不幸的是,它们不是正则表达式,功能也不太强大。特别是它们不支持交替(Java或Perl正则表达式的垂直条
|
操作符),这正是您想要做的

一个简单的解决方法可以是:

local function MatchAny( str, pattern_list )
    for _, pattern in ipairs( pattern_list ) do
        local w = string.match( str, pattern )
        if w then return w end
    end
end


s = "hello dolly!"
print( MatchAny( s, { "hello", "world", "%d+" } ) )

s = "cruel world!"
print( MatchAny( s, { "hello", "world", "%d+" } ) )

s = "hello world!"
print( MatchAny( s, { "hello", "world", "%d+" } ) )

s = "got 1000 bucks"
print( MatchAny( s, { "hello", "world", "%d+" } ) )
输出:

hello world hello 1000 你好 世界 你好 1000
函数
MatchAny
将其第一个参数(一个字符串)与Lua模式列表相匹配,并返回第一次成功匹配的结果。

仅扩展peterm的建议,lpeg还提供了一个
re
模块,该模块公开了与lua的标准
string
库类似的接口,同时仍然保留了lpeg提供的额外功能和灵活性

我想说,首先尝试一下
re
模块,因为它的语法与lpeg相比不那么深奥。下面是一个与hello world示例相匹配的示例用法:

dump = require 'pl.pretty'.dump
re = require 're'


local subj = "hello, world! padding world1 !hello hello hellonomatch nohello"
pat = re.compile [[
  toks  <-  tok (%W+ tok)*
  tok   <-  {'hello' / 'world'} !%w / %w+
]]

res = { re.match(subj, pat) }
dump(res)
如果您对捕获匹配的位置感兴趣,只需稍微修改语法以进行位置捕获:

tok   <-  {}('hello' / 'world') !%w / %w+

tok使用逻辑运算符和Lua模式可以解决大多数问题。例如,对于正则表达式
[hello | world]%d+
,可以使用

string.match(str, "hello%d+") or string.match(str, "world%d+")

操作符的快捷电路确保字符串首先匹配
hello%d+
,如果失败,则匹配
世界%d+

我不确定使用内置模式匹配是否可以轻松完成。如果您需要使用Lua执行复杂的解析/匹配,您可以尝试lpeg:@peterm是的,使用lpeg很容易<代码>(lpeg.P(“foo”)+“bar”):匹配(输入)
string.match(str, "hello%d+") or string.match(str, "world%d+")