Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 Regex Capture Groups By Name

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?

like image 781
Travis Parks Avatar asked Jun 02 '13 20:06

Travis Parks


People also ask

How do I capture a group in regex?

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".

Can I use named capture groups?

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.

What is regex match group?

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.

What does capture mean in regex?

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.


1 Answers

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.

like image 189
aaronman Avatar answered Sep 20 '22 07:09

aaronman