我对boost::regex::regex_match
有疑问。我开启工作BOOST_REGEX_MATCH_EXTRA
。
我有什么:
(这是我的问题的一个简单示例,而不是真正的任务)
string input1= "3 4 5";
string input2= "3 4 7";
我想要的是什么:
list output1= [3 4 5];
list output2= []; //not matched
正则表达式:
(这个工作正常)
((?<group>[0-6])[ ]?)*
output1:what["group"]=5
和what["group"].captures()= [3, 4, 5]
output2:not matched
问题是:
我需要将多于一个部分正则表达式中的数据收集到一个组中。
我试过了:
((?<group>[0-6])[ ])*(?<group>[0-6])
output1:what["group"]=4
和what["group"].captures()=[3, 4]
output2:not matched
好的,我明白了。它没有看到第二组声明。
我试过了:
((?<group>[0-6])[ ])*(?&group)
output1:what["group"]=4
和what["group"].captures()= [3, 4, 4]
output2:not matched
我有一组以上,所以token_iterator无法帮助我。
表达式应该在配置文件中配置。静态Xpressive不能使用。
答案 0 :(得分:0)
这就是我解释你的问题的方法:
字符串:
Total price: $1,234
并且您希望将费用捕获为1234
(不使用逗号)
仅使用正则表达式无法做到这一点,因为无法捕获组并排除中间的部分。话虽这么说,您可以使用2个匹配组和前瞻,然后在内部代码将组拼接在一起。使用上面的例子,如果您不知道是否会有逗号(即价格范围为1-5000),您可以执行类似
的操作 Total price: \$(?P<price>\d{1,3})(?:(?=\,),(?P<price2>\d{3})|)
匹配1-3位数字,然后查找逗号,如果存在,则使用不同的名称组并匹配第二个数据块。
这是一个非常好的正则表达式测试资源:www.regex101.com