要给出一个不匹配单词的正则表达式,可以使用负向匹配(negative lookahead)来实现。
正则表达式:(?i)\b(?!word)\w+\b
这个正则表达式使用了(?i)
来表示不区分大小写,\b
用于匹配单词的边界,(?!word)
表示后面不能跟着"word"这个单词,\w+
用于匹配一个或多个字母数字字符,\b
表示单词边界。
以下是一个示例代码,使用Python的re模块来进行匹配:
import re
regex = r'(?i)\b(?!word)\w+\b'
text = "This is a sample sentence. It contains words like apple, banana, and orange."
matches = re.findall(regex, text)
for match in matches:
print(match)
输出结果为:
This
is
a
sample
sentence
It
contains
like
apple
banana
and
orange
可以看到,正则表达式成功地过滤掉了单词"word"。