排除单词的正则表达式
我正在尝试写一个正则表达式,只匹配字符串中的第一个和第三个单词:
term1 and term2
我第一次尝试用的是 [^(\s|(and))]+
,但这个方法不行,因为
term1 anbd term2
它给了我这三个匹配结果: ['term1','b','term2']
,而我想要的是 ['term1','and','term2']
5 个回答
1
你可以使用这个正则表达式 \b\w+\b
来把你的句子按单词分开,然后取第一个和第三个单词。
import re
pat = re.compile(r'\b\w+\b') # pre-compile the pattern
# for this example the pre-compiling doesn't really matter.
temp = re.findall(pat, "Hello, beautiful world!")
lst = [temp[0], temp[2]] # sets lst to ["Hello", "world"]
3
与其使用正则表达式,不如考虑
sentence.split()[:3:2]
例如
>>> "term1 and term2".split()[:3:2]
['term1', 'term2']
>>> "term1 anbd term2".split()[:3:2]
['term1', 'term2']
>>>
5
只匹配第一个和第三个单词: (\S+)\s+\S+\s+(\S+)
补充说明: 如果你的意思是“匹配所有单词,但不包括‘and’这个词”,那么可以用: \b(?!and\b)\S+\b