Mark Galeck
Mark Galeck

Reputation: 6395

In Python regexes, why can't I match 0-or-more, multiple times

In Python, why does this bomb:

>>>re.compile(r'(?:.*){1}')

when this works

>>>re.compile(r'(?:.+){1}')

I just checked, in Perl, both work fine. Now I don't like Perl. I don't need Perl. But this is a regular-expression engine question. Why doesn't Python engine understand such a simple thing?

Upvotes: 0

Views: 78

Answers (3)

Julien Spronck
Julien Spronck

Reputation: 15433

Same reason why you can't do

re.compile(r'{1}')

There is or might be nothing to repeat once.

I think that the following regex should do the same as what you intended:

re.compile(r'((?:)|(?:.+){1})')

Upvotes: 1

beerbajay
beerbajay

Reputation: 20270

These both work for me:

Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> p1 = re.compile(r'(?:.*){1}')
>>> m1 = p1.match("foo")
>>> m1.group()
'foo'
>>> p2 = re.compile(r'(?:.+){1}')
>>> m2 = p2.match("foo")
>>> m2.group()
'foo'

Upvotes: 3

DhruvPathak
DhruvPathak

Reputation: 43245

The first regex is trying to repeat 1 instance of something which may not exist, hence an invalid regex. The second one will always give a non-null string repeated once.

For example, in the first regex , it is not being checked how many times the null instance will be repeated, in the second it is, that is where it gives the error.

>>> re.compile(r'(?:.*)xyz')
<_sre.SRE_Pattern object at 0x1a867b0>
>>> re.compile(r'(?:.*){1}')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/re.py", line 190, in compile
    return _compile(pattern, flags)
  File "/usr/lib/python2.7/re.py", line 242, in _compile
    raise error, v # invalid expression
sre_constants.error: nothing to repeat

Upvotes: 2

Related Questions