我想在字符串中找到找到的匹配词的5个上游词。 例。我有琴弦
这是有史以来最荒谬的老鼠
我要搜索“老鼠”,然后获取找到的“老鼠”字词上游的4个字
我尝试使用
re.search(r'\brat\b', " This is the most Absurd rat in the history")
但是它给了我像span(25,28)这样的空间位置,但是我将如何使用它来获取单词。如果我想知道单词的位置,那么我可以简单地获得4个索引的上/下单词。
答案 0 :(得分:2)
(?:\S+\s){4}(?=rat\b)
可能接近您想要的:
>>> sentence = "This is the most Absurd rat in the history"
>>> import re
>>> re.findall(r'(?:\S+\s){4}(?=rat\b)', sentence, re.I)
['is the most Absurd ']
>>> re.findall(r'(?:\S+\s){4}(?=rat\b)', "I like Bratwurst", re.I)
[]
>>> re.findall(r'(?:\S+\s){4}(?=rat\b)', "A B C D rat D E F G H rat", re.I)
['A B C D ', 'E F G H ']
这里是example。
答案 1 :(得分:1)
您可以使用re.findall
:
s = "This is the most Absurd rat ever in the history"
print(re.findall('^[\w\W]+(?=\srat)', s)[0].split()[-4:])
输出:
['is', 'the', 'most', 'Absurd']
编辑2:
如果您要查找在"rat"
后面出现的四个单词,则可以使用itertools.groupby
:
import itertools
s = "Some words go here rat This is the most Absurd rat final case rat"
new_data = [[a, list(b)] for a, b in itertools.groupby(s.split(), key=lambda x:x.lower() == 'rat')]
if any(a for a, _ in new_data): #to ensure that "rat" does exist in the string
results = [new_data[i][-1][-4:] for i in range(len(new_data)-1) if new_data[i+1][0]]
print(results)
输出:
[['Some', 'words', 'go', 'here'], ['is', 'the', 'most', 'Absurd'], ['final', 'case']]
答案 2 :(得分:1)
编辑:由于要查找rat
之前的所有单词,因此需要使用更复杂的正则表达式的findall
:
import re
s = 'This is the most absurd rat ever in the history of rat kind I tell you this rat is ridiculous.'
answer = [sub.split() for sub in re.findall(r'((?:\S+\s*){4})rat', s)]
# [['is', 'the', 'most', 'absurd'],
# ['in', 'the', 'history', 'of'],
# ['I', 'tell', 'you', 'this']]
上级答案:
您可以通过split
通过rat
字符串:
import re
s = 'This is the most Absurd rat ever in the history'
answer = re.split(r'\brat\b', s, 1)[0].split()[-4:]
# => ['is', 'the', 'most', 'Absurd']
我假设上游是之前,如果您是之后,则将[0]
更改为[1]
,将[-4:]
更改为[:4]
。您还需要添加一些代码来检查rat
是否在字符串中甚至是偶数,否则将中断。