Python正则表达式-获取匹配周围的单词

Python正则表达式-获取匹配周围的单词,python,regex,Python,Regex,我想在比赛前后都听到这些话。我可以使用string.split(“”)——但既然我已经使用了正则表达式,难道没有更好的方法只使用正则表达式吗 使用匹配对象,我可以得到确切的位置。但是,此位置是字符索引的 import re myString = "this. is 12my90\nExample string" pattern = re.compile(r"(\b12(\w+)90\b)",re.IGNORECASE | re.UNICODE) m

我想在比赛前后都听到这些话。我可以使用
string.split(“”)
——但既然我已经使用了正则表达式,难道没有更好的方法只使用正则表达式吗

使用匹配对象,我可以得到确切的位置。但是,此位置是字符索引的

import re

myString = "this. is 12my90\nExample string"
pattern = re.compile(r"(\b12(\w+)90\b)",re.IGNORECASE |  re.UNICODE)

m = pattern.search(myString)
print("Hit: "+m.group())
print("Indix range: "+str(m.span()))
print("Words around match: "+myString[m.start()-1:m.end()+1]) # should be +/-1 in _words_, not characters
输出:

点击:2012年9月12日Indix

范围:(9,15)

比赛周边词:2009年12月12日

为了获得匹配的单词和之前的单词,我尝试:

pattern = re.compile(r"(\b(w+)\b)\s(\b12(\w+)90\b)",re.IGNORECASE | 
re.UNICODE)

在第二种模式中,您必须像
\w+
一样退出
w+

除此之外,您的示例中还有一个新行,您可以使用下面的另一个
\s

3个捕获组的模式可能如下所示

(\b\w+\b)\s(\b12\w+90\b)\s(\b\w+\b)

您可以使用捕获组来获取值

print("Words around match: " + m.group(1) + " " + m.group(3))

缺少新行字符

regx = r"(\w+)\s12(\w+)90\n(\w+)"


\s
包括换行符。