Context
I am trying to get this code work. The idea is to detect if at least one of the characters in the joined
string are present in the string s
.
#include<regex>
#include<string>
#include<iostream>
int main()
{
std::string s = "b";
std::string joined = " ,;()[]";
bool result = std::regex_search(s, std::regex("^[^" joined "]"));
std::cout << result << std::endl;
return 0;
}
Problem
I struggle having this code (or some of its variants) compile and run across different compilers.
It seems to works as expected with gcc 12, does not compile on clang 13 from C.E, and my Apple Clang 13.1.6 (macbook M1) compiles but throws the following exception:
fatal error: in "newick_formatting/trivial_label": std::__1::regex_error: The parser did not consume the entire regular expression.
Minimal reproductible example on Compiler explorer
https://godbolt.org/z/P4KdG3asf
Is there a way to achieve the desired result with less chaos? :)
CodePudding user response:
This looks like an escaping issue, where the ]
from joined
is closing the character class, leaving you with a trailing ]
which the libc regex parser doesn't like.
For the contents of a character class, you'd want to escape ]
at any position as \\]
(that is a literal backslash followed by ]
), as well as -
unless it is in the first position.
For this specific case, changing joined
to be joined = " ,;()[\\]";
will make it compile and run correctly.