What is the semantic difference between "^(?P<x>\d?)" and "^(?P<x>\d)?"?

I have been working with long regular expressions used to extract information from input strings where some fields are optional and I wonder if there is a practical difference between:

^(?P<x>\d)?

^(?P<x>\d?)

I know they are not exactly the same expression since the former is an optional expression whereas the later is a non-optional expression containing an optional expression but both expressions give the same result. Is there any difference I am not seeing? Is any of them more efficient than the other when using python "re" module?

Upvotes: 1

Views: 82

Answers (1)

georg
georg

Reputation: 214949

Let me be your terminal:

Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> re.match("^(?P<x>\d)?(?P<rest>.)", "abc").groupdict()
{'x': None, 'rest': 'a'}
>>> re.match("^(?P<x>\d?)(?P<rest>.)", "abc").groupdict()
{'x': '', 'rest': 'a'}
>>> 

In other words, (\d?) always succeeds and matches an empty string if there are no digits, while (\d)? can fail and return a None group.

Upvotes: 3

Related Questions