regular expression doesnt work while execute in c
regular expression doesnt work while execute in c
i am trying to build regular expression with the regex.h lib.
i checked my expression in https://regex101.com/ with the the input
"00001206 ffffff00 00200800 00001044" and i checked it in python as well, both gave me the expected result.
when i ran the code below in c (over unix) i got "no match" print.
any one have any suggest?
regex_t regex;
int reti;
reti = regcomp(®ex, "([0-9a-fA-F]8( |$))+$", 0);
if (reti)
fprintf(stderr, "Could not compile regexn");
exit(1);
reti = regexec(®ex, "00001206 ffffff00 00200800 00001044", 0, NULL, 0);
if (!reti)
printf("Match");
else if (reti == REG_NOMATCH)
printf("No match bla blan");
1 Answer
1
Your pattern contains a $
anchor, capturing groups with (...)
and the interval quantifier m,n
, so you need to pass REG_EXTENDED
to the regex compile method:
$
(...)
m,n
REG_EXTENDED
regex_t regex;
int reti;
reti = regcomp(®ex, "([0-9a-fA-F]8( |$))+$", REG_EXTENDED); // <-- See here
if (reti)
fprintf(stderr, "Could not compile regexn");
exit(1);
reti = regexec(®ex, "00001206 ffffff00 00200800 00001044", 0, NULL, 0);
if (!reti)
printf("Match");
else if (reti == REG_NOMATCH)
printf("No match bla blan");
See the online C demo printing Match
.
Match
However, I believe you need to match the entire string, and disallow whitespace at the end, so probably
reti = regcomp(®ex, "^[0-9a-fA-F]8( [0-9a-fA-F]8)*$", REG_EXTENDED);
will be more precise as it will not allow any arbitrary text in front and won't allow a trailing space.
@YonatanAmir I am really certain this is not related to the regex. Please check how to read Unicode files in C. It might be related to BOM. Make sure you do not keep it when passing the contents to the regex engine.
– Wiktor Stribiżew
Sep 4 '18 at 14:41
sorry for the spam it was 'n'
– Yonatan Amir
Sep 4 '18 at 14:45
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
thanks dude, i have one more Q, i am reading this data from file but when i am trying to read i get no match.how can i check if there is any other invisible characters ?
– Yonatan Amir
Sep 4 '18 at 14:35