mercredi 6 mai 2015

How to re-match a group that did not capture anything?

I'm trying to parse a string in which a certain section can either be enclosed between " or ' or not be enclosed at all. However, I'm struggling finding a syntax that works when no quotation marks are there at all.

See the following (simplified) example:

>>> print re.match(r'\w(?P<quote>(\'|"))?\w', 'f"oo').group('quote')
"

>>> print re.match(r'\w(?P<quote>(\'|"))?\w', 'foo').group('quote')
None

>>> print re.match(r'\w(?P<quote>(\'|"))?\w(?P=quote)', 'f"o"o').group('quote')
"

>>> print re.match(r'\w(?P<quote>(\'|"))?\w(?P=quote)', 'foo').group('quote')
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "<string>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
'NoneType' object has no attribute 'group'

The desired result for the last attempt should be None as the second command in the example.

Aucun commentaire:

Enregistrer un commentaire