Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ regex escaping punctional characters like "."

Matching a "." in a string with the std::tr1::regex class makes me use a weird workaround.

Why do I need to check for "\\\\." instead of "\\."?

regex(".") // Matches everything (but "\n") as expected.
regex("\\.") // Matches everything (but "\n").
regex("\\\\.") // Matches only ".".

Can someone explain me why? It's really bothering me since I had my code written using boost::regex classes, which didn't need this syntax.

Edit: Sorry, regex("\\\\.") seems to match nothing.

Edit2: Some code

void parser::lex(regex& token)
{
    // Skipping whitespaces
    {
        regex ws("\\s*");
        sregex_token_iterator wit(source.begin() + pos, source.end(), ws, regex_constants::match_default), wend;
        if(wit != wend)
            pos += (*wit).length();
    }

    sregex_token_iterator it(source.begin() + pos, source.end(), token, regex_constants::match_default), end;
    if (it != end)
        temp = *it;
    else
        temp = "";
}
like image 695
Tim Avatar asked Dec 16 '22 16:12

Tim


1 Answers

This is because \. is interpreted as an escape sequence, which the language itself is trying to interpret as a single character. What you want is for your regex to contain the actual string "\.", which is written \\. because \\ is the escape sequence for the backslash character (\).

like image 101
Agentlien Avatar answered Jan 04 '23 10:01

Agentlien