我最近在尝试匹配类似于1-4,5,9,20-25
. 诚然,生成的正则表达式并不简单:
/\G([0-9]++)(?:-([0-9]++))?+(?:,(?=[-0-9,]+$))?+/
这个表达式允许我逐步收集字符串中的所有匹配项。
我们可以对您的问题应用相同的方法,但要验证和匹配您的给定输入非常困难。(我不知道该怎么做。如果其他人这样做,我想看看!)但是您可以单独验证输入:
/\(\s*(\s*((\s*\d+\s+\d+\s*)\)\s*)+\s*\)/
请参阅Evan 的答案以了解其工作原理。\d
等价于[0-9]
和\s
等价于[\r\n\t ]
。
这是提取数字的增量匹配:
/\G\(?\s*(?:\(\s*(\d+)\s+(\d+)\s*\))(?:(?=\s*\(\s*\d+\s+\d+\s*\))|\s*\))/
它像这样分解:
/\G # matches incrementally. \G marks the beginning of the string or the beginning of the next match.
\(?\s* # matches first open paren; safely ignores it and following whiespace if this is not the first match.
(?: # begins a new grouping - it does not save matches.
\(\s* # first subgroup open paren and optional whitespace.
(\d+) # matches first number in the pair and stores it in a match variable.
\s+ # separating whitespace
(\d+) # matches second number in the pair and stores it in a match variable.
\s*\) # ending whitespace and close paren
) # ends subgroup
(?: # new subgroup
(?= # positive lookahead - this is optional and checks that subsequent matches will work.
\s*\(\s*\d+\s+\d+\s*\) # look familiar?
) # end positive lookahead
| # if positive lookahead fails, try another match
\s*\)\s* # optional ending whitespace, close paren
)/ # ... and end subgroup.
我还没有测试过这个,但我相信它会起作用。每次将表达式应用于给定字符串时,它都会提取每个后续的数字对,直到它看到最后一个右括号,它会消耗整个字符串,或者如果出现输入错误则停止。您可能需要对 Boost::regex 进行优化。这是一个与 Perl 兼容的正则表达式。