Last active
October 14, 2021 19:02
-
-
Save moreati/f1167727291401af96becd76c8b50418 to your computer and use it in GitHub Desktop.
Trying regular epxressions purported to match double quoted strings with backslash escapes
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
#!/usr/bin/env python3 | |
""" | |
Trying regular epxressions purported to match double quoted strings with backslash escapes | |
Syntax changes made vs sources | |
- Changed capture groups to non-capture groups () -> (?:) | |
- Removed escaping of double quotes \" -> " | |
""" | |
import re | |
CANDIDATES = [ | |
# https://stackoverflow.com/questions/249791/regex-for-quoted-string-with-escaping-quotes | |
(r'"(?:\\.|[^\"])*"', 'https://stackoverflow.com/a/1016356', None), | |
(r'"(?:[^"\\]*(?:\\.[^"\\]*)*)"', 'https://stackoverflow.com/a/10786066', None), | |
# https://stackoverflow.com/questions/5695240/php-regex-to-ignore-escaped-quotes-within-quotes | |
(r'"(?:[^"\\]|\\.)*"', 'https://stackoverflow.com/a/5696141', 'good') | |
(r'"(?:(?=[^"\\]+)|\\.)*"', 'https://stackoverflow.com/a/5696141', 'better'), | |
(r'"[^"\\]*(?:\\.[^"\\]*)*"', 'https://stackoverflow.com/a/5696141', 'best'), | |
# https://github.com/lark-parser/lark | |
# _STRING_INNER : /.*?/ | |
# _STRING_ESC_INNER : _STRING_INNER /(?<!\\)(\\\\)*?/ | |
# ESCAPED_STRING : "\"" _STRING_ESC_INNER "\"" | |
(r'".*?(?<!\\)(?:\\\\)*?"', 'https://github.com/lark-parser/lark/blob/ebb15f75721a161feb293c4dcdc613c04539d9eb/lark/grammars/common.lark', None), | |
] | |
if __name__ == '__main__': | |
for i, (expr, url) in enumerate(CANDIDATES): | |
try: | |
pattern = re.compile(expr, re.DOTALL) | |
except re.error: | |
print(i, expr) | |
raise | |
print(i, pattern.findall(r' "", "\"", "\\", "\n" ')) |
Author
moreati
commented
Oct 13, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment