Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for string matching with special escaping rule

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.

like image 420
monoceres Avatar asked Nov 20 '25 04:11

monoceres


2 Answers

regex = re.compile(r'"(?:[^"]|"")*"')

This just finds the literals, it doesn't decode them by replacing the doubled quotes.

like image 155
morningstar Avatar answered Nov 22 '25 17:11

morningstar


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']
like image 40
Jon Clements Avatar answered Nov 22 '25 18:11

Jon Clements



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!