Skip to content

Instantly share code, notes, and snippets.

@B1Z0N
Forked from prathe/gist:2439752
Created April 14, 2020 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save B1Z0N/028d1667f4f576dac79b4453f07cd065 to your computer and use it in GitHub Desktop.
Save B1Z0N/028d1667f4f576dac79b4453f07cd065 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