在lua中创建一个匹配括号和字符串的模式

我想创建一个可以匹配像(figure这样的字符串的模式。

我尝试了

string.find("See this example (figure 1), "%(%figure$")

但它不起作用。

点赞
用户3832970
用户3832970

你的 %(%figure$ 模式是无效的,会抛出

missing '[' after '%f' in pattern

因为 %f 定义了一个 _frontier pattern_。

你可以使用

string.match("See this example (figure 1)", "%((figure%s*%d+)%)")

Lua demo online

详情

  • %( - 一个 ( 字符
  • (figure%s*%d+) - 捕获组(这个值将会是 string.match 的输出):figure,零或多个空格( %s*)和一个或多个数字( %d+
  • %) - 一个 ) 字符
2020-06-05 08:41:58