Skip to content

Instantly share code, notes, and snippets.

@kurzweil777
Last active May 20, 2020 07:47
Show Gist options
  • Save kurzweil777/fe536d0e74c8c312a3c893b00d777b1f to your computer and use it in GitHub Desktop.
Save kurzweil777/fe536d0e74c8c312a3c893b00d777b1f to your computer and use it in GitHub Desktop.
Regular Expressions
import re
phrases = ("Alice eats apples.\n"
"Bob pets cats.\n"
"Carol throws baseballs.\n"
"Alice throws Apples.\n"
"BOB EATS CATS.\n"
"RoboCop eats apples.\n"
"ALICE THROWS FOOTBALLS.\n"
"Carol eats 7 cats.")
phrasesRegex = re.compile(r'''^(Alice|Bob|Carol)\s(eats|pets|throws)\s(apples|cats|baseballs)''', re.I | re.MULTILINE)
result = phrasesRegex.findall(phrases)
print(result)
# [('Alice', 'eats', 'apples'),
# ('Bob', 'pets', 'cats'),
# ('Carol', 'throws', 'baseballs'),
# ('Alice','throws', 'Apples'),
# ('BOB', 'EATS', 'CATS')]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment