Last active
December 25, 2015 14:58
-
-
Save ProProgrammer/6994472 to your computer and use it in GitHub Desktop.
Formulating Regular Expressions 1 (Challenge and their solutions) - Sharing amazing learnings from Udacity's CS262 - Programming Languages - https://www.udacity.com/course/cs262.Strict Advise: Do Attempt Challenge before you see solution
This file contains 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
# RE Challenges | |
# Assign to the variable regexp a Python regular expression that matches single- | |
# argument mathematical functions. | |
# The function name is a lowercase word (a-z), the function argument must be a | |
# number (0-9), and there may optionally be spaces before and/or after the | |
# argument. | |
# Hint: You may need to escape the ( and ). | |
import re | |
regexp = _________________ (Fill in the blank Space) | |
# regexp matches: | |
print re.findall(regexp,"cos(0)") == ["cos(0)"] | |
#>>> True | |
print re.findall(regexp,"sqrt( 2 )") == ["sqrt( 2 )"] | |
#>>> True | |
# regexp does not match: | |
print re.findall(regexp,"cos (0)") != ["cos (0)"] | |
#>>> True | |
print re.findall(regexp,"sqrt(x)") != ["sqrt(x)"] | |
#>>> True | |
This file contains 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
regexp = r"[a-z]+\( *[0-9] *\)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment