英文:
How to exclude linebreaks from a regex match in python?
问题
<br>
如何使下面的正则表达式排除跨越多行的匹配?
import re
reg = re.compile(r'\b(apple)(?:\W+\w+){0,4}?\W+(tree|plant|garden)')
reg.findall('my\napple tree in the garden')
reg.findall('apple\ntree in the garden')
第一个应该匹配,第二个不应该匹配。<br>
(现在两者都匹配...)
英文:
<br>
How can I make the bellow regex exclude matches that span across lines?
import re
reg = re.compile(r'\b(apple)(?:\W+\w+){0,4}?\W+(tree|plant|garden)')
reg.findall('my\napple tree in the garden')
reg.findall('apple\ntree in the garden')
The first one should match, the second one should not.<br>
(Now both matches...)
答案1
得分: 1
你的 \W
匹配换行符。要排除它们,请用 [^\w\n]
替换 \W
:
import re
reg = re.compile(r'\b(apple)(?:[^\n\w]+\w+){0,4}?[^\n\w]+(tree|plant|garden)')
print(reg.findall('my\napple tree in the garden'))
# [('apple', 'tree')]
print(reg.findall('apple\ntree in the garden'))
# []
英文:
Your \W
matches newlines. To exclude them replace \W
with [^\w\n]
:
import re
reg = re.compile(r'\b(apple)(?:[^\n\w]+\w+){0,4}?[^\n\w]+(tree|plant|garden)')
print(reg.findall('my\napple tree in the garden'))
# [('apple', 'tree')]
print(reg.findall('apple\ntree in the garden'))
# []
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论