Python 捕获两个单词之间的字符串,但仅捕获第一次
我有这样的字符串:Python 捕获两个单词之间的字符串,但仅捕获第一次,python,regex,Python,Regex,我有这样的字符串: text = "Why do Humans need to eat food? Humans eat food to survive." 我想捕捉人类和食物之间的一切,但这只是第一次 预期产出 Humans need to eat food 我的正则表达式: p =r'(\bHumans?\b.*?\bFoods?\b)' re.findall(p, text, re.I|re.M|re.DOTALL) ('Humans need to eat food',) P
text = "Why do Humans need to eat food? Humans eat food to survive."
我想捕捉人类
和食物
之间的一切,但这只是第一次
预期产出
Humans need to eat food
我的正则表达式:
p =r'(\bHumans?\b.*?\bFoods?\b)'
re.findall(p, text, re.I|re.M|re.DOTALL)
('Humans need to eat food',)
Python代码:
p =r'(\bHumans?\b.*?\bFoods?\b)'
re.findall(p, text, re.I|re.M|re.DOTALL)
('Humans need to eat food',)
代码正确地捕获了人类和食物之间的字符串,但它不会在第一次捕获时停止
研究:
我已经读过,为了让它不贪婪,我需要把?
放在哪里,但我不知道应该把它放在哪里才能让它不贪婪。我尝试过的所有其他排列和组合在第一场比赛中都无法阻止
更新
我正在编写大量正则表达式来捕获像这样的各种其他实体并一次性解析它们,因此我无法更改我的
re.findall
逻辑。使用search
而不是findall
:
import re
text = "Why do Humans need to eat food? Humans eat food to survive."
p =r'(\bHumans?\b.*?\bFoods?\b)'
res = re.search(p, text, re.I|re.M|re.DOTALL)
print(res.groups())
输出:
p =r'(\bHumans?\b.*?\bFoods?\b)'
re.findall(p, text, re.I|re.M|re.DOTALL)
('Humans need to eat food',)
或者在正则表达式末尾添加*
:
import re
text = "Why do Humans need to eat food? Humans eat food to survive."
p =r'(\bHumans?\b.*?\bFoods?\b).*'
# here ___^^
res = re.findall(p, text, re.I|re.M|re.DOTALL)
print(res)
使用
search
而不是findall
:
import re
text = "Why do Humans need to eat food? Humans eat food to survive."
p =r'(\bHumans?\b.*?\bFoods?\b)'
res = re.search(p, text, re.I|re.M|re.DOTALL)
print(res.groups())
输出:
p =r'(\bHumans?\b.*?\bFoods?\b)'
re.findall(p, text, re.I|re.M|re.DOTALL)
('Humans need to eat food',)
或者在正则表达式末尾添加*
:
import re
text = "Why do Humans need to eat food? Humans eat food to survive."
p =r'(\bHumans?\b.*?\bFoods?\b).*'
# here ___^^
res = re.findall(p, text, re.I|re.M|re.DOTALL)
print(res)
对于仅查找第一个匹配项,Toto的答案是最好的,但正如您所说,您只需要使用
findall
,您可以在正则表达式末尾附加*
,以匹配剩余的文本,这不会导致任何进一步的匹配
(\bHumans?\b.*?\bFoods?\b).*
^^ This eats remaining part of your text due to which there won't be any further matches.
Python代码示例
import re
text = "Why do Humans need to eat food? Humans eat food to survive."
p =r'(\bHumans?\b.*?\bFoods?\b).*'
print(re.findall(p, text, re.I|re.M|re.DOTALL))
印刷品
['Humans need to eat food']
对于仅查找第一个匹配项,Toto的答案是最好的,但正如您所说,您只需要使用
findall
,您可以在正则表达式末尾附加*
,以匹配剩余的文本,这不会导致任何进一步的匹配
(\bHumans?\b.*?\bFoods?\b).*
^^ This eats remaining part of your text due to which there won't be any further matches.
Python代码示例
import re
text = "Why do Humans need to eat food? Humans eat food to survive."
p =r'(\bHumans?\b.*?\bFoods?\b).*'
print(re.findall(p, text, re.I|re.M|re.DOTALL))
印刷品
['Humans need to eat food']
试试这个:
>>> import re
>>> text = "Why do Humans need to eat food? Humans eat food to survive."
>>> re.search(r'Humans.*?food', text).group() # you want the all powerful non-greedy '?' :)
'Humans need to eat food'
试试这个:
>>> import re
>>> text = "Why do Humans need to eat food? Humans eat food to survive."
>>> re.search(r'Humans.*?food', text).group() # you want the all powerful non-greedy '?' :)
'Humans need to eat food'
你试过这个
re.findall(p,text,re.I | re.M | re.DOTALL)[0]
?实际上我正在编写很多正则表达式并一次解析它们,因此我无法更改我的re.findall
逻辑re.findall
捕获所提供的文本中的所有匹配项,它不必对多个正则表达式做任何事情,它一次只使用一个正则表达式。@xbound不是使用re.findall
然后选择它的第一个匹配项,re.search
将是一个更好的选择,因为它不会搜索所有匹配项,并且会在找到第一个匹配项后停止,显然会更快。@MuhammadAhmad:我知道re.findall
一次只能使用一个正则表达式,但我需要用同一段代码编译其他几个正则表达式,因此无法实现@xbound解决方案或使用re.search
。你试过re.findall(p,text,re.I | re.M | re.DOTALL)[0]
?事实上,我正在编写大量正则表达式并一次性解析它们,因此我无法更改我的re.findall
逻辑re.findall
捕获所提供的文本中的所有匹配项,它不必对多个正则表达式执行任何操作,它一次只使用一个正则表达式。@xbound不是使用re.findall
然后选择它的第一个匹配项,re.search
将是一个更好的选择,因为它不会搜索所有匹配项,并且会在找到第一个匹配项后停止,显然会更快。@MuhammadAhmad:我知道re.findall
一次只能使用一个正则表达式,但我需要用同一段代码编译其他几个正则表达式,因此无法实现@xbound解决方案或使用re.search
。谢谢你的回答,事实上,我正在编写大量正则表达式并一次性解析它们,因此我无法更改我的re.findall
逻辑谢谢你的回答,事实上我正在编写大量正则表达式并一次性解析它们,因此我无法更改我的re.findall
logicOne更多帮助请,在同一个示例中,如果我只需要使用re.findall()
获取needtoeat
。只能通过修改正则表达式来实现吗?@RahulAgarwal:您可以使用\bneed to eat\b
正则表达式来获取该字符串的所有实例,但为什么要这样做?你能说出你想要达到的目标吗?显示一些字符串并告诉预期的输出?我想要两个单词之间的字符串,就像我上面的问题一样,但不想要起始词和结束词。所以,像text=“为什么人类需要吃食物?人类吃食物是为了生存。”
但是答案应该是需要吃,而不是人类需要吃食物,比如说,在人类
和食物
之间,如果匹配的文本中没有这些词,您需要使用积极的向前看和向后看。谢谢你的帮助!!如果我只需要使用re.findall()
获取need to eat
,请再给我一个帮助。只能通过修改正则表达式来实现吗?@RahulAgarwal:您可以使用\bneed to eat\b
正则表达式来获取该字符串的所有实例,但为什么要这样做?你能说出你想要达到的目标吗?显示一些字符串并告诉预期的输出?我想要两个单词之间的字符串,就像我上面的问题一样,但不想要起始词和结束词。所以,像text=“为什么人类需要吃食物?人类吃食物是为了生存。”
但是答案应该是需要吃,而不是人类需要吃食物,比如说,在人类
和食物
之间,如果匹配的文本中没有这些词,您需要使用积极的向前看和向后看。谢谢你的帮助!!