Problem with STL regex

Stan Malachowski 51 Reputation points
2023-05-09T09:52:50.5033333+00:00

Hi,

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

(?<=<abc>)(.|\n|\r\n)*?(?=</abc>)

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?

Thanks,

Stan

C++
C++
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.
3,519 questions
{count} votes

Accepted answer
  1. Viorel 111.8K Reputation points
    2023-05-09T12:47:53.3466667+00:00

    The “lookbehind” assertions (?<= and (?!< are not allowed in STL: https://learn.microsoft.com/en-us/cpp/standard-library/regular-expressions-cpp.

    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 51 Reputation points
    2023-05-09T10:21:45.9266667+00:00

    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 51 Reputation points
    2023-05-09T10:51:56.4833333+00:00

    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

    Regards,

    Stan

    0 comments No comments