How to exclude linebreaks from a regex match in python?

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.
(Now both matches…)

>Solution :

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'))
#  []

Leave a Reply