Skip to content

Instantly share code, notes, and snippets.

@tristanlatr
Created March 17, 2022 20:36
Show Gist options
  • Save tristanlatr/c6d02cb23b275ab8b674e5893fc95890 to your computer and use it in GitHub Desktop.
Save tristanlatr/c6d02cb23b275ab8b674e5893fc95890 to your computer and use it in GitHub Desktop.
Utility function to unquote a string. It supports all kinds of python quotes.
import re
import ast
_QUOTED_STR_REGEX = re.compile(r'(^\"(?:\\.|[^\"\\])*\"$)|'
r'(^\'(?:\\.|[^\'\\])*\'$)')
# older version (not very strong): (^\'\'\'(\s+)?(((?!\'\'\').)*)?(\s+)?\'\'\'$)
_TRIPLE_QUOTED_STR_REGEX = re.compile(r'(^\"\"\"(\s+)?(([^\"]|\"([^\"]|\"[^\"]))*(\"\"?)?)?(\s+)?(?:\\.|[^\"\\])?\"\"\"$)|'
# Unescaped quotes at the end of a string generates
# "SyntaxError: EOL while scanning string literal",
# so we don't account for those kind of strings as quoted.
r'(^\'\'\'(\s+)?(([^\']|\'([^\']|\'[^\']))*(\'\'?)?)?(\s+)?(?:\\.|[^\'\\])?\'\'\'$)', flags=re.DOTALL)
def _is_quoted(text:str) -> bool:
return bool(_QUOTED_STR_REGEX.match(text)) or \
bool(_TRIPLE_QUOTED_STR_REGEX.match(text))
def unquote_str(text:str) -> str:
"""
Unquote a maybe quoted string representation.
If the string is not detected as being a quoted representation, it returns the same string as passed.
It supports all kinds of python quotes: C{\"\"\"}, C{'''}, C{"} and C{'}.
"""
if _is_quoted(text):
s = ast.literal_eval(text)
assert isinstance(s, str)
return s
return text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment