Skip to content

Instantly share code, notes, and snippets.

@prathe
Created April 21, 2012 21:33
Show Gist options
  • Save prathe/2439752 to your computer and use it in GitHub Desktop.
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
# 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