Pattern issue with boost::regex
Hi All, I have a question regarding boost::regex pattern recognition and groups I created the following code, in it i created a pattern and a string to match: 1 2 3 4 5 6 p = "((a b (f+\\.))d d(\\3))"; str="a b ffff.d dfff."; boost::regex pattern(p, boost::regex::icase&boost::regex::extended &~boost::regex::no_bk_refs); bool match = boost::regex_match(str, pattern); cout << str << ((match)?"-Pattern is legal":"-Pattern is illegal" )<< endl; Pattern is illegal I'm saw that my result is 'Pattern is illegal' because the group \\3 that refers to (f+\\.) isn't equal to it. if I change the input to str="a b ffff.d dffff."; The pattern is legal. In other words my original plan was to use the \\3 instead of writing the pattern; p = "((a b (f+\\.))d d(f+\\.))"; But I can see that in the original pattern it simply expect the same output to reappear Is there a way I can use the group naming not to copy the (f+\\.) group? Thanks, I hope my Q is clear since I'm new to the regex topic.
Strictly speaking, regular expressions do not include back references. Modern regular expression matchers actually implement a superset of the "regular languages". I am fairly sure no extension includes the kind of back reference you are looking for. Bear in mind that you can construct your regular expression string at runtime. You might consider doing something like the following: char p[100]; char *sub = "f+\\."; sprintf(p, "((a b (%s))d d(%s))", sub, sub); Steven J. Clark VGo Communications From: Boost-users [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Tal Shnaiderman Sent: Thursday, October 23, 2014 4:12 AM To: boost-users@lists.boost.org Subject: [Boost-users] Pattern issue with boost::regex Hi All, I have a question regarding boost::regex pattern recognition and groups I created the following code, in it i created a pattern and a string to match: 1 2 3 4 5 6 p = "((a b (f+\\.))d d(\\3)file:///\\3))"; str="a b ffff.d dfff."; boost::regex pattern(p, boost::regex::icase&boost::regex::extended &~boost::regex::no_bk_refs); bool match = boost::regex_match(str, pattern); cout << str << ((match)?"-Pattern is legal":"-Pattern is illegal" )<< endl; Pattern is illegal I'm saw that my result is 'Pattern is illegal' because the group \\3file:///\\3 that refers to (f+\\.) isn't equal to it. if I change the input to str="a b ffff.d dffff."; The pattern is legal. In other words my original plan was to use the \\3file:///\\3 instead of writing the pattern; p = "((a b (f+\\.))d d(f+\\.))"; But I can see that in the original pattern it simply expect the same output to reappear Is there a way I can use the group naming not to copy the (f+\\.) group? Thanks, I hope my Q is clear since I'm new to the regex topic.
In other words my original plan was to use the \\3 instead of writing the pattern;
p = "((a b (f+\\.))d d(f+\\.))";
But I can see that in the original pattern it simply expect the same output to reappear
Is there a way I can use the group naming not to copy the (f+\\.) group?
There are two "kinds" of back references in regular expressions: the "traditional" kind \n matches the same *text* as matched by group #n. However, Perl (and Boost.Regex) have a newer extension called recursive expressions, these execute a regular expression located at grouping n using the (?n) syntax, see http://www.boost.org/doc/libs/1_56_0/libs/regex/doc/html/boost_regex/syntax/... HTH, John.
participants (3)
-
John Maddock
-
Steven Clark
-
Tal Shnaiderman