【发布时间】:2019-03-10 15:40:17
【问题描述】:
对于像"{foo}{bar}" 这样的字符串有一个简单的
str = "{foo}{bar}"
first, second = str:gmatch(...)...
应该给first="foo"和second="bar"
问题是foo本身可以有更多的括号,例如:
str = "{foo {baz}{bar}"
这样first = "foo {baz"
bar 部分只有字母数字字符,没有括号
【问题讨论】:
-
first, second = str:match('({[^}]*})%s*({[^}]*})')呢? -
对于
{foo}{bar},我会得到first="{foo}"和second="{bar}",但它应该是first="foo"和second="bar",没有外部大括号 -
我错误地移动了捕获括号:
first, second = str:match('{([^}]*)}%s*{([^}]*)}')。见ideone.com/aFroK5 -
是的,很好:-)。请给它作为答案
标签: string lua pattern-matching lua-patterns