Last active
May 20, 2020 07:47
-
-
Save kurzweil777/fe536d0e74c8c312a3c893b00d777b1f to your computer and use it in GitHub Desktop.
Regular Expressions
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
| 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