21

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?

Travis Parks
  • 8,435
  • 12
  • 52
  • 85
  • Have you tried `(?P.*?)` instead of `(?.*?)` ? – HamZa Jun 02 '13 at 20:46
  • @HamZa I am not sure if C++11 regex supports named capture groups. I just read the entire chapter dedicated to the topic in Stroustrup's new "The C++ Programming Language" and it doesn't even mention it. And many of the online references don't seem to indicate otherwise. – Travis Parks Jun 02 '13 at 21:14
  • It seems it doesn't support named groups according to this [answer](http://stackoverflow.com/a/6398650). – HamZa Jun 02 '13 at 21:45

1 Answers1

18

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.

aaronman
  • 18,343
  • 7
  • 63
  • 78