Moti
Moti

Reputation: 927

find series of number in C linux using regular expression

I used the code below that uses the "{}" but seems its not working as expected when using C.

int basic_regx(char *format, char *name)
{
     regex_t     regex;
     char        array[100];
     if( regcomp(&regex, format, 0) )
         return; 
     if( !regexec(&regex, name, 0, NULL, 0) )
         printf ("Succeeded\n");
     else 
         printf ("Not Succeeded\n");
     regfree(&regex);    
 }

If I call function with follwoing:

Success - basic_regx("^[0-9]$","0");
Not Success -- basic_regx("^[0-9]{1,4}$","0");
Success - basic_regx("^[0-9]{1,4}$","0{1,4}");

Thats mean the {} is not taken as expected by the reg implementation.

Upvotes: 1

Views: 56

Answers (1)

John Chadwick
John Chadwick

Reputation: 3213

regcomp and regexec use POSIX regular expressions; in other words, it only supports features described in POSIX. If you want Perl-style regular expressions, which support this expression, you may need an external library such as PCRE.

However, with POSIX regular expressions, you can do the equivalent:

basic_regx("^[0-9]\\{1,4\\}$","0");

Upvotes: 2

Related Questions