Problem with STL regex

Stan Malachowski 41 Reputation points


I am having trouble with the following regular expression in the C++ STL for Visual Studio 2022.


This is intended to match everything between the tags <abc> and </abc> . I have checked several online regexp analysers, and they indicate that it's fine. The STL throws a cryptic exception when construting a regex object.

Can anyone spot the issue?



A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
2,895 questions
{count} votes

Accepted answer
  1. Viorel 94,511 Reputation points

    The “lookbehind” assertions (?<= and (?!< are not allowed in STL:

    For simple cases, try an alternative:

    string s = "... <abc>some text</abc> ...";
    regex r( "(?:<abc>)((.|\\n|\\r\\n)*?)(?:</abc>)" );
    smatch m;
    if( regex_search( s, m, r ))
        string result = m[1];
        // . . .

2 additional answers

Sort by: Most helpful
  1. Stan Malachowski 41 Reputation points

    The relevant lines are below. The regex line throws an exception. Does this suffice? Thanks

    	auto r = regex("(?<=<xyzzy>)(.|\n|\r\n)*?(?=</xyzzy>)");
    	s = regex_replace(s, r, "$1");

  2. Stan Malachowski 41 Reputation points

    Hi Viorel,

    Thanks. That's answered my question. Is there a reference for STL not having that construct?

    What I am trying to do is extract everything between two tags:

    blah blah.....***<abc>****This is the text I want</abc>***blah... blah



    0 comments No comments