I am converting my boost-based regular expressions to C++11 regex. I have a capture group called url
:
\s*?=\s*?(("(?<url>.*?)")|('?<url>.*?)'))
With boost, if you had an smatch
you could call match.str("url")
to get the capture group by name. With std::smatch
, I am only seeing indexed sub-matches.
How can I get access to the url capture using the std::smatch class?
Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d", "o", and "g".
Mixing named and numbered capturing groups is not recommended because flavors are inconsistent in how the groups are numbered. If a group doesn't need to have a name, make it non-capturing using the (?:group) syntax.
Regular expressions allow us to not just match text but also to extract information for further processing. This is done by defining groups of characters and capturing them using the special parentheses ( and ) metacharacters. Any subpattern inside a pair of parentheses will be captured as a group.
capturing in regexps means indicating that you're interested not only in matching (which is finding strings of characters that match your regular expression), but you're also interested in using specific parts of the matched string later on.
You cannot name a capture group with the c++11 standard. C++11 regex conforms to the ECMAScript syntax. Here is a link that explains it all http://www.cplusplus.com/reference/regex/ECMAScript/. Even though this maybe disappointing if you think about it a true regular expression will not support this it is extra.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With