I'm trying to match special kind of string literals with some funky escaping rules.
The general form looks like this:
"some string"
Which are simple to match using a pattern such as "(.*?)"
However you can escape quotes by doubling them, such as:
"hello "" there" becomes hello " there
"hello """" there" becomes hello "" there
And this is where my regex skills fail me. How can I match strings like this?
Oh, and I'm using python 3.1.
regex = re.compile(r'"(?:[^"]|"")*"')
This just finds the literals, it doesn't decode them by replacing the doubled quotes.
Not using a regular expression, but you've specified Python, so here's a way to get your expected output:
>>> import csv
>>> strings = ['"some string"', '"hello "" there"', '"hello """" there"']
>>> for s in strings:
print next(csv.reader([s]))
['some string']
['hello " there']
['hello "" there']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With