I’m trying to parse a file in which quotation files are used to encapsulate strings. For instance, the file might contain a line like this:
"\"Hello there, my friends,\" the tour guide says." me @ swap notify
But it might also contain lines like this:
"I'm a dingus who wants to put a backslash at the end of my statements. \\" me @ swap notify
In that example, the quotes shouldn’t be escaped, but a single backslash should remain.
Is there any function I can use to extract that full quoted statement? \n for newline and \r for carriage return also show up on occasion, so I’d like to get those two, but only after I have the full string isolated.
ast.literal_evalthe string and assign it to a variable.Test:
The regex says “Match anything and store it as group 1, up to a space, a word, a space, @-sign, space and a word”. You then retreive the group with the
.group(1)syntax. The parenthesis define a group, see regex documentation.Here’s a version that tries to parse the string as greedily as possible, by failing and retrying until a match is found, or no match can be made: