C++ regex escaping punctional characters like "."

17,092

Solution 1

As it turns out, the actual problem was due to the way sregex_token_iterator was used. Using match_default meant it was always finding the next match in the string, if any, even if there is a non-match in-between. That is,

string source = "AAA.BBB";
regex dot("\\.");
sregex_token_iterator wit(source.begin(), source.end(), dot, regex_constants::match_default);

would give a match at the dot, rather than reporting that there was no match.

The solution is to use match_continuous instead.

Solution 2

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 (\).

Share:
17,092

Related videos on Youtube

Tim
Author by

Tim

Updated on September 17, 2022

Comments

  • Tim
    Tim over 1 year

    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 = "";
    }
    
  • Tim
    Tim over 11 years
    See my example code, it just doesn't work the way you said. Besides it seems like "\\\\." doesn't work either.
  • Jakub Zaverka
    Jakub Zaverka over 11 years
    @Tim can you show us the whole code you are using to match the string?
  • Tim
    Tim over 11 years
    I did add the primary function. Thanks for helping out.
  • Agentlien
    Agentlien over 11 years
    For posterity: Tim had some formatting issues (SO interpreting \\ as \ inside posts) which led to the confusion behind my answer. However, having tried myself, \\. does work as expected in Visual Studio 2012, using std::regex.
  • Tim
    Tim over 11 years
    Actually it's not quite the problem. The problem was that it doesn't match end since the string contains still characters. So a simple replacement of if (it != end) to if (it != end && *it != "") fixed everything!