Why is my C++ regex not matching?

  • Context: C/C++ 
  • Thread starter Thread starter cheers00
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around a C++ regular expression that is not matching as expected. Participants are exploring the reasons behind the mismatch, focusing on the syntax of the regex and potential issues with the string being matched.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant notes that their regex works in notepad2 but fails in their C++ code, questioning the reason for the mismatch.
  • Another participant points out that "\s" is a nonstandard escape sequence and suggests using "\\s" to properly denote the backslash followed by 's'.
  • A different participant raises the possibility that the string may be terminated with an ASCII null character, which could affect the regex match.
  • Several participants emphasize the need to double the backslashes in the regex to avoid misinterpretation by the compiler, suggesting that the regex engine sees "s+" instead of "\s+".
  • One participant confirms that doubling the backslashes resolved their issue.

Areas of Agreement / Disagreement

Participants generally agree on the need to double the backslashes in the regex. However, there are differing opinions regarding other potential issues, such as the presence of an ASCII null character.

Contextual Notes

There are unresolved assumptions regarding the exact nature of the input string and how it may affect regex matching. The discussion also highlights the importance of understanding escape sequences in C++.

Who May Find This Useful

Readers interested in C++ programming, particularly those working with regular expressions and string manipulation, may find this discussion relevant.

cheers00
Messages
5
Reaction score
0
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;
}
 
Technology news on Phys.org
"\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.
 
cheers00 said:
I am wondering why this is not matching. I tested my regular expression on notepad2 and it matches fine.


Code:
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;
 }

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[/color]] tags. Now your spaces are appearing.
 
  • Like
Likes   Reactions: 1 person
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.
 
Ibix said:
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.
Your explanation makes more sense than mine...
Ibix said:
Edit: must type quicker.
 
Ibix said:
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.

doubling back slash worked. thank you
 
!{a problem} :D
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 118 ·
4
Replies
118
Views
10K
Replies
89
Views
7K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 5 ·
Replies
5
Views
1K
Replies
73
Views
6K
  • · Replies 5 ·
Replies
5
Views
7K