Quantcast
Viewing all articles
Browse latest Browse all 11

C++11 Regular Expression

Syed Babu wrote:

Let me put my question in another way

regex r1("<.*>.*<.*>");
regex r2("<(.*)>(.*)</(\\1)>");

What does the r1 search for and what does the r2 search for?

Can you give with an example?In the second case, we are using the  capture group as we wanted to refer it later in the regex (
like \1 )

That's not the only use for a capture group - perhaps not even the  primary use. If that were your only reason, why did you specify three  pairs of parenthses, when you only refer to the first one? Why not  "<(.*)>.*</\\1>" ? But there is more to capture groups than back  references.

After a successful call to regex_search( str,s2,r2), you can write  s2[1].str(), s2[2].str() and s2[3].str() to find out which string  matched the first, second and third capture group (in your original  example, you'd get "book","C++" and "book", correspondingly). That's  what capture groups are mainly for.


Igor Tandetnik


Viewing all articles
Browse latest Browse all 11

Trending Articles