1

I have to parse a regular expression which can contain special symbols such as \s and \d. The problem is, I can't distinguish the \ when i am parsing the expression, I mean '\s' == 's', therefore I cannot distinguish between special character and basic character. How can I solve this?

2
  • probably regex101.com can help you? Commented Jan 31, 2017 at 10:37
  • No. I have a given expression: a\sb which will be different from asb, but i cannot catch the single *\* to parse it properly Commented Jan 31, 2017 at 10:42

2 Answers 2

2

Raw string literals since C++11 can help you to improve the readability:

"a\\sb" // matches: a[whitespace]b "a\\\\sb" // matches: a\sb 

becomes:

R"(a\sb)" // matches: a[whitespace]b R"(a\\sb)" // matches: a\sb 
Sign up to request clarification or add additional context in comments.

1 Comment

I think your second examples should read "matches: a\\sb" - note the "s" before the "b"
2

You're confusing user input and character literals. You catch the user input \ by comparing all input characters with the character literal '\\'.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.