C++11 Regex Capture Groups By Name

14,312

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.

Share:
14,312

Related videos on Youtube

Travis Parks
Author by

Travis Parks

I'm a developer who spends most of his time writing business applications in .NET. I especially enjoy computing theory, application design and good programming practices. I try to keep my skills up-to-date and keep my knowledge broad. I love creating open source projects; check me out on GitHub. I consider myself an expert at .NET, software design, automation and large-scale distributed enterprise architectures.

Updated on September 15, 2022

Comments

  • Travis Parks
    Travis Parks over 1 year

    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?

    • HamZa
      HamZa almost 11 years
      Have you tried (?P<url>.*?) instead of (?<url>.*?) ?
    • Travis Parks
      Travis Parks almost 11 years
      @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.
    • HamZa
      HamZa almost 11 years
      It seems it doesn't support named groups according to this answer.
  • Travis Parks
    Travis Parks almost 11 years
    Can you post an example extracting a nested group? Perhaps where the match can be on either side of a pipe?
  • Travis Parks
    Travis Parks almost 11 years
    I basically want to see how to efficiently extract my url based on my posted example, updated to not rely on named capture groups.
  • Dilawar
    Dilawar almost 3 years
    This makes me sad!