RE: [Boost-Users] Regexp Homework :-)
Simon Brown
A few days ago I received great help with a regexp question - now here's another which I doubt can be done.
Question: if the text string contains three words AAA BBB AAA then return the string (if any) which is repeated two or three times.
Examples: AAA AAA AAA return AAA AAA BBB AAA return AAA AAA AAA BBB return AAA AAA BBB CCC return <nothing>
Can this be done with a regular expression?
Match against "(\\w+) (?:\\1 \\w+|\\w+ \\1)|\\w+ (\\w+) \\2" and return the match for whichever sub-expression (1 or 2) was matched. (I tested this with Python not Boost regex, but they should be compatible.)
Wow! Thanks for the fast answer, I'm using C++ but now that you show me how
to do this I wonder why I didn't figure it out myself :-)
Simon Brown, Casa Bergenia, CH-7031 Laax
www.hb9drv.ch
----- Original Message -----
From: "Ben Hutchings"
Match against "(\\w+) (?:\\1 \\w+|\\w+ \\1)|\\w+ (\\w+) \\2" and return the match for whichever sub-expression (1 or 2) was matched.
(I tested this with Python not Boost regex, but they should be compatible.)
participants (2)
-
Ben Hutchings
-
Simon Brown