Unknown escape sequence ?

Mar 15, 2009 at 4:49pm
Hi Guys,

I am using a regular expression
 
"^[0-9]\{3\}\.[0-9]\{3\}\.[0-9]\{2,3\}\.[0-9]\{1,3\}";

It keeps saying that unknown escape sequence. The weird thing is, it was just giving warnings until I included <algorithm.h> file. As soon as I included this header, it started giving error in that regex stating, unknown escape sequence.
Any thoughts?

Thanks in advance
Mar 15, 2009 at 4:54pm
\ is a special character which lets you input 'escape sequences' that let you input text that would otherwise be impossible to input (as well as other things). For example if you wanted to have text with quotes:

"text"

your first thought would be to input the text like so:

""text""

but of course this wouldn't work because the " character would end the string, leaving you with two empty strings with a 'text' identifier between them. Escape sequences provide a way around this. \" is treated as one character (the " character) when put in a string. So to accomplish the above:

"\"text\""

Of course, this now means that \ is a special character. Therefore if you want to input it in your string, you need to change all of those \ to \\:

"^[0-9]\\{3\\}\\ ... etc
Mar 15, 2009 at 5:01pm
It's so weird ... I tried this exact same thing the other day and it didn't worked. But it worked today, I am sure I was doing something wrong before.

Thanks a lot for the help Disch :)
Mar 15, 2009 at 5:16pm
Oops ... now it says that the pattern does not match, whereas it was matching before. (Since I am running this code on my computer, where it was working with a single \ and was not working on school's computer. But now it says that 1**.***.**.** is not in a valid format ?

Edit: typo
Last edited on Mar 15, 2009 at 5:16pm
Mar 15, 2009 at 5:19pm
I don't know anything about regex, but could it be that you are supposed to be using / and not \?

That might explain why it was working with one / before.
Mar 15, 2009 at 5:40pm
Why not start by posting a sample pattern or two that you want to match?
Mar 15, 2009 at 5:43pm
192.168.1.102
Mar 15, 2009 at 5:52pm
Try:

\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}

\d matches digits [0-9]
{min,max} denotes a range of repetitions
\. matches a period
Last edited on Mar 15, 2009 at 5:54pm
Mar 15, 2009 at 5:59pm
Same error man

1
2
3
4
5
6
7
8
9
10
11
12
13
14
regexch.cc: In function ‘int main()’:
regexch.cc:10: error: unknown escape sequence '\d'
regexch.cc:10: error: unknown escape sequence '\.'
regexch.cc:10: error: unknown escape sequence '\d'
regexch.cc:10: error: unknown escape sequence '\.'
regexch.cc:10: error: unknown escape sequence '\d'
regexch.cc:10: error: unknown escape sequence '\.'
regexch.cc:10: error: unknown escape sequence '\d'
regexch.cc:10: warning: deprecated conversion from string constant to ‘char*’

shell returned 1

Press ENTER or type command to continue
Mar 15, 2009 at 6:00pm
Just in case, this is the code I am using

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<regex.h>

using namespace std;

int main() {
char myIP[] = "192.111.123.1";
char *regexNamePattern;   // Used to check the pattern of IP address  

  regexNamePattern = "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}";

  regex_t preg;
  int     regexCheck;
  char    errorText[100];

if (regexCheck = regcomp(&preg, regexNamePattern, REG_EXTENDED)) {
cout<<"True"<<endl;
  }
  else if (regexCheck = regexec(&preg, myIP, 0, NULL, 0)) {
    regerror(regexCheck, &preg, errorText, 100);

 }
  else {
    cout<<"Argument passed to Validate_IP is NULL"<<endl;

  }


return 0;
}


Edit: Notes
Last edited on Mar 15, 2009 at 6:01pm
Mar 15, 2009 at 6:11pm
Remember that if you want to have a \ char in C++ you have to use \\
"^\\d{1,3}\\.\\d ..."
Mar 15, 2009 at 6:26pm
Yeah but I don't want to include \ I just want to use it as a escape sequence. Is there any way we can turn off these type of errors. Like, the same code works perfectly all right on Mac OS Terminal ( it just gives it as warning saying that unknown sequence), but those warnings are changed to errors and does not work on linux terminal.
Mar 15, 2009 at 6:43pm
Yeah but I don't want to include \ I just want to use it as a escape sequence


No you don't want the escape sequence, you want the actual \ character. Put in \\

Is there any way we can turn off these type of errors.

No. In C/C++, if you want to have a string containing a \ character, it must have \\ because a single \ starts an escape sequence.

Mac OS Terminal


Terminal != C++


perhaps I should clarify this further:

Strings that you put in quotes are not exact copies of the strings they represent. If they were, it would be impossible to represent some characters (like the " character, as previously mentioned). Therefore C/C++ compilers step through these strings and look for escape sequences to translate them into seperate characters.

For example '\n' is an escape sequence which represents a new line (ASCII code 0x0A). This is seperate from both the '\' character (ASCII code 0x5C) and 'n' character (0x6E).

When you put '\d' or '\.' in a string, the compiler looks at that and sees the \ so it starts an escape sequence, but then looks at the next characters and says "hey, this isn't a valid escape sequence!". This is what is giving you the compiler warning. And since the compiler couldn't step through the string properly, the string your sending to your regex lib is probably bad, which is causing the mismatching (giving you errors).
Last edited on Mar 15, 2009 at 6:51pm
Mar 15, 2009 at 6:49pm
Oops, I am sorry, what I meant to say was compiler.
Topic archived. No new replies allowed.