flex中的正则表达式可以匹配除以外的任何单词

时间:2022-11-13 09:37:14

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

我有这个Flex的regex,可以找到每两个字母大写的单词:

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

But how can i print every word except SN ??

但是,除了SN,我怎么打印出每个单词呢?

Thanks!!

谢谢! !

1 个解决方案

#1


3  

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

您的regex也将匹配子字符串,例如在字符串NASA中使用的NA和SA。

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

你需要使用单词边界来防止这种情况发生。然后,使用一个否定的前视断言排除SN:

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

Edit: Oh, that Flex :)

编辑:哦,那个Flex:)

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

POSIX regex引擎不知道如何查找。你需要把它拼出来:

\b(S[A-MO-Z]|[A-RT-Z]N|[A-MO-RT-Z]{2})\b

#1


3  

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

您的regex也将匹配子字符串,例如在字符串NASA中使用的NA和SA。

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

你需要使用单词边界来防止这种情况发生。然后,使用一个否定的前视断言排除SN:

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

Edit: Oh, that Flex :)

编辑:哦,那个Flex:)

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

POSIX regex引擎不知道如何查找。你需要把它拼出来:

\b(S[A-MO-Z]|[A-RT-Z]N|[A-MO-RT-Z]{2})\b