Created
January 16, 2020 21:30
-
-
Save altwitt/2c94958f78cf244994a35eca90c7b40f to your computer and use it in GitHub Desktop.
6 handy regex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Finding sentences that contain a specific word | |
import re | |
def sentences_with_word(word, words): | |
p = re.compile('/[^.]* {} [^.]*\./gi'.format(word)) | |
print(p.search(words)) | |
s1 = ''' | |
FROM fairest creatures we desire increase, | |
That thereby beauty's rose might never die, | |
But as the riper should by time decease, | |
His tender heir might bear his memory: | |
But thou, contracted to thine own bright eyes, | |
Feed'st thy light'st flame with self-substantial fuel, | |
Making a famine where abundance lies, | |
Thyself thy foe, to thy sweet self too cruel. | |
Thou that art now the world's fresh ornament | |
And only herald to the gaudy spring, | |
Within thine own bud buriest thy content | |
And, tender churl, makest waste in niggarding. | |
Pity the world, or else this glutton be, | |
To eat the world's due, by the grave and thee. | |
''' | |
sentences_with_word("thy", s1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment