Created
April 21, 2012 21:33
-
-
Save prathe/2439752 to your computer and use it in GitHub Desktop.
Python Regular Expression: double-quoted string literals that allows for escaped double quotes
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
# Tricky REs with ^ and \ | |
# Assign to regexp a regular expression for double-quoted string literals that | |
# allows for escaped double quotes. | |
# Hint: Escape " and \ | |
# Hint: (?: (?: ) ) | |
import re | |
regexp = r'"(?:[^\\]|(?:\\.))*"' | |
# regexp matches: | |
print re.findall(regexp,'"I say, \\"hello.\\""') == ['"I say, \\"hello.\\""'] | |
#>>> True | |
# regexp does not match: | |
print re.findall(regexp,'"\\"') != ['"\\"'] | |
#>>> True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment