Python Regex - Sentence Not Including Strings
I have a series of sentences I am trying to decipher. Here are two examples: Valid for brunch on Saturdays and Sundays and Valid for brunch I want to compose a regex that identif
Solution 1:
^(?!.*saturday)(?!.*sunday).*(brunch)
You can try in this way.Grab the capture.See demo.
Solution 2:
use a list comprehension , if you have all the sentences in a list like sentences
you can use the following comprehension :
import re
[re.search(r'\bbranch\b',s) for s in sentences if `saturday` notin s and'sunday'notin s ]
Solution 3:
I would do like this,
>>> sent = ["Valid for brunch on Saturdays and Sundays", "Valid for brunch"]
>>> sent
['Valid for brunch on Saturdays and Sundays', 'Valid for brunch']
>>> for i in sent:
ifnot re.search(r'(?i)(?:saturday|sunday)', i) and re.search(r'brunch', i):
print(i)
Valid for brunch
Post a Comment for "Python Regex - Sentence Not Including Strings"