Question

I have this regex in Flex to find every 2 letter capitalized word:

[A-Z]{2}        printf("Found %s", yytext);

But how can i print every word except SN ??

Thanks!!

Was it helpful?

Solution

Your regex will also match substrings, for example NA and SA when used on the string NASA.

You will need to use word boundaries to prevent that from happening. Then, use a negative lookahead assertion to exclude SN:

\b(?!SN)[A-Z]{2}\b

Edit: Oh, that Flex :)

Well, POSIX regex engines don't know lookarounds. You'll need to spell it out:

\b(S[A-MO-Z]|[A-RT-Z]N|[A-MO-RT-Z]{2})\b
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top