在lua中创建一个匹配一个括号和字符串的模式
我想创建一个匹配字符串的模式,如在lua中创建一个匹配一个括号和字符串的模式,lua,lua-patterns,Lua,Lua Patterns,我想创建一个匹配字符串的模式,如(图) 我试过了 string.find("See this example (figure 1), "%(%figure$") 但是它不起作用。您的%(%figure$模式无效,它会抛出 missing '[' after '%f' in pattern 因为%f定义了一个 你可以用 string.match("See this example (figure 1)", "%((figure%s*%d+)%)") 看 详细信息 %(-a(字符 (图%s*
(图)
我试过了
string.find("See this example (figure 1), "%(%figure$")
但是它不起作用。您的%(%figure$
模式无效,它会抛出
missing '[' after '%f' in pattern
因为%f
定义了一个
你可以用
string.match("See this example (figure 1)", "%((figure%s*%d+)%)")
看
详细信息
%(
-a(
字符
(图%s*%d+)
-捕获组(此值将是字符串的输出。匹配
):图
,零个或多个空格(%s*
),然后是1+位(%d+
)
%
-a)
字符
如果不是字符串,则不应使用$
finish@DamienDotta顺便说一句,仅供参考:如果您只需要提取数字,请将括号移到%d
之前:%(图%s*(%d+))
确定,如果我只需要提取(图
不带空格和数字?@DamienDotta同样,调整捕捉括号的位置,((图)%s*%d+%
。或者,如果要匹配(图
),请使用%(图
。因为(
和)
用于定义模式中的捕获组,您需要在它们前面加上%
以使它们成为文字字符。