Boost regex. Named group in two part -
i have problem boost::regex::regex_match
. work turned on boost_regex_match_extra
.
what have:
(this simple example of problem, not real task)
string input1= "3 4 5"; string input2= "3 4 7";
what want get:
list output1= [3 4 5]; list output2= []; //not matched
regex:
(this working ok)
((?<group>[0-6])[ ]?)*
output1: what["group"]=5
, what["group"].captures()= [3, 4, 5]
output2: not matched
the problem is:
i need collect data more one part of regex 1 group.
i tried:
((?<group>[0-6])[ ])*(?<group>[0-6])
output1: what["group"]=4
, what["group"].captures()=[3, 4]
output2: not matched
ok, understand. doesn't see second declaration of group.
i tried:
((?<group>[0-6])[ ])*(?&group)
output1: what["group"]=4
, what["group"].captures()= [3, 4, 4]
output2: not matched
- but this? second 4 from? checks "group" pattern, because first example matches, second doesn't. doubles last found value instead of saving new. why? maybe forgot turn on flags?
- and there way in 1 group data different part of regex expression?
i have more 1 group, token_iterator can't me.
and expression should configured in config file. static xpressive can't used.
this how interpret problem:
string: total price: $1,234
and want capture cost 1234
(without comma)
this isn't possible regex, there no way capture group , exclude parts in middle. being said, can use 2 match groups , lookaheads, , inside code stitch groups together. using above example, if don't know whether there comma or not (i.e. price ranges 1-5000) can
total price: \$(?p<price>\d{1,3})(?:(?=\,),(?p<price2>\d{3})|)
which matches 1-3 digits, comma, , if exists, use different name group , match second chunk.
here's nice resource regex testing: www.regex101.com
Comments
Post a Comment