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(&regex, "([0-9a-fA-F]8( |$))+$", 0);
if (reti)

fprintf(stderr, "Could not compile regexn");
exit(1);


reti = regexec(&regex, "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(&regex, "([0-9a-fA-F]8( |$))+$", REG_EXTENDED); // <-- See here
if (reti)

fprintf(stderr, "Could not compile regexn");
exit(1);


reti = regexec(&regex, "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(&regex, "^[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.





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






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

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)