warning: unknown escape sequence '\

44,076

In C string literals the \ has a special meaning, it's for representing characters such as line endings \n. If you want to put a \ in a string, you need to use \\.

For example

"\\Hello\\Test"

will actually result in "\Hello\Test".

So your regexp needs to be written as:

"[0-9]\\{1,3\}\\\\.[0-9]\\{1,3\}\\\\.[0-9]\\{1,3\\}\\\\.[0-9]\\{1,3\\}"

instead of:

"[0-9]\{1,3\}\\.[0-9]\{1,3\}\\.[0-9]\{1,3\}\\.[0-9]\{1,3\}"

Sure this is painful because \ is used as escape character for the regexp and again as escape character for the string literal.

So basically: when you want to put a \ you need to write \\.

Share:
44,076
Santhosh Pai
Author by

Santhosh Pai

Interested in embedded systems ,c and c++ programming.I'm here to learn.

Updated on February 04, 2020

Comments

  • Santhosh Pai
    Santhosh Pai over 4 years

    I'm trying to run a regex through a system command in the code, I have gone through the threads in StackOverflow on similar warnings but I couldn't understand on how to fix the below warnings, it seems to come only for the closed brackets on doing \\}. The warnings seem to disappear but not able to get the exact output in the redirected file.

    #include<stdio.h>
    
    
    int main(){
    
            FILE *in;
            char buff[512];
    
    if(system("grep -o '[0-9]\{1,3\}\\.[0-9]\{1,3\}\\.[0-9]\{1,3\}\\.[0-9]\{1,3\}' /home/santosh/Test/text >t2.txt") < 0){
                    printf("system failed:");
                    exit(1);
            }
    }
    

    Warnings:

    dup.c:9:11: warning: unknown escape sequence '\}'
    dup.c:9:11: warning: unknown escape sequence '\}'
    dup.c:9:11: warning: unknown escape sequence '\}'
    dup.c:9:11: warning: unknown escape sequence '\}'
    dup.c: In function 'main':
    
  • Santhosh Pai
    Santhosh Pai over 7 years
    still the same problem
  • Jabberwocky
    Jabberwocky over 7 years
    Forgot two `\` s. Question has been edited. Try again.