Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying regex, simple example won't work

Tags:

c

regex

This is my first time working with regular expressions. What I need is something that matches u<float>v<float>, but for now I'll settle for something that matches u<float> with the float being in the open unit interval. This neat site got me to the following expression:

u0.\d+ "match u, zero, a dot, and one or more digits"

Then I wanted to try it in C, as below:

#include <stdio.h>
#include <sys/types.h>
#include <regex.h>

int main(int argc, char **argv)
{
    regex_t regex;
    char *regex_str = "u0.\\d+";
    regcomp(&regex, regex_str, 0);

    char *str = "u0.51";

    int status = regexec(&regex, str, (size_t)0, NULL, 0);
    printf("Status: %d\n", status);

    return 0;
}

Now, according to man regexec, regexec returns zero on match. The above returns 1. What's wrong? Thank you.

like image 846
Erik Vesterlund Avatar asked Dec 03 '25 20:12

Erik Vesterlund


1 Answers

You need escape the . as it is a regular expression meta character that matches any one character. To escape it, prefix it with \. However \ is a meta char in C string literals well, so you must escape that, therefore getting \\..

\d is a Perl-compatible regular expression idiom that matches a single digit. It is not present in POSIX regular expressions - thus you must use [0-9] instead. Lastly, + is not a metacharacter in basic regular expressions - you must use REG_EXTENDED. Therefore we have

const char *regex_str = "u0\\.[0-9]+";
regcomp(&regex, regex_str, REG_EXTENDED);


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!