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(®ex, regex_str, 0);
char *str = "u0.51";
int status = regexec(®ex, 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.
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(®ex, regex_str, REG_EXTENDED);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With