Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

warning: unknown escape sequence '\

Tags:

c

regex

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':
like image 956
Santhosh Pai Avatar asked Apr 25 '26 11:04

Santhosh Pai


1 Answers

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 \\.

like image 140
Jabberwocky Avatar answered Apr 28 '26 04:04

Jabberwocky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!