[英]Boost regex. Named group in two part
我有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不能使用。
這就是我解釋你的問題的方法:
字符串: Total price: $1,234
並且您希望將成本捕獲為1234
(不含逗號)
僅使用正則表達式是不可能的,因為無法捕獲組並排除中間的部分。 話雖這么說,您可以使用2個匹配組和前瞻,然后在內部代碼將組拼接在一起。 使用上面的例子,如果你不知道是否會有逗號(即價格范圍從1-5000)你可以做類似的事情
Total price: \\$(?P<price>\\d{1,3})(?:(?=\\,),(?P<price2>\\d{3})|)
哪個匹配1-3個數字,然后查找逗號,如果存在,則使用不同的名稱組並匹配第二個塊。
這是一個非常好的正則表達式測試資源: www.regex101.com
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.