Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ regex not matching

  1. Oct 9, 2013 #1
    I am wondering why this is not matching. I tested my regular expression on notepad2 and it matches fine.


    std::string line = " 10 0 5 0";
    std::regex rgx("\s+\d+\s+\d+\s+\d+\s+\d+");

    if (regex_match(line, rgx))
    {
    // do something...
    int a = 2;
    }
     
  2. jcsd
  3. Oct 9, 2013 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    "\s" is a single character string. Containing what character? Who knows. That backslash s is a nonstandard escape sequence. Your code won't even compile on my computer. You need to use "\\s" to denote a backslash followed by an 's'. The same goes for all of your other single backslashes.
     
  4. Oct 9, 2013 #3

    Mark44

    Staff: Mentor

    The only thing that occurs to me is that the string is terminated with an ASCII null char, possibly looking like \0, and your regex match string doesn't take that into account.

    BTW, I replaced your font tags with [code] tags. Now your spaces are appearing.
     
  5. Oct 9, 2013 #4

    Ibix

    User Avatar
    Science Advisor

    Double up those \. The compiler is interpreting \s as a special character, so the regex engine is seeing something like s+, not \s+. Use \\s and \\d and you'll be fine.

    Edit: must type quicker.
     
  6. Oct 9, 2013 #5

    Mark44

    Staff: Mentor

    Your explanation makes more sense than mine...
     
  7. Oct 9, 2013 #6
    doubling back slash worked. thank you
     
  8. Oct 9, 2013 #7

    Ibix

    User Avatar
    Science Advisor

    !{a problem} :D
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C++ regex not matching
Loading...