problem with re

Roman Suzi rnd at onego.ru
Thu Sep 6 14:18:42 EDT 2001


On Thu, 6 Sep 2001, Patrick Vrijlandt wrote:

>Hi,
>
>I do not understand why the 7th regular expression does not give the same
>result as 2-6.
>
>(This is simplified from something I'm working on. The answer may be
>trivial, but I don't see it)
>
>TIA,
>Patrick Vrijlandt
>
>import re
>
>data = """1234567890
>abcdefghij
>1234567890"""
>
>def mymatch(pattern):
>    mo = re.match(pattern, data)
>    print mo,
>    try:
>        print mo.groups()
>    except AttributeError:
>        print
>
>
>mymatch('.*\\n.*\\n.*')
>mymatch('(.*)\\n(.*)\\n(.*)')
>mymatch('(.{10})\\n(.{10})\\n(.{10})')
>mymatch('(\d{10})\\n([a-z]{10})\\n(\d{10})')
>mymatch(r'(\d{10})\n([a-z]{10})\n(\d{10})')
>mymatch(r'\n'.join([r'(\d{10})', r'([a-z]{10})', r'(\d{10})']))
>mymatch(r'\n'.join([r'(\d{10})|([a-z]{10})', r'([a-z]{10})', r'(\d{10})']))

You forgot to put () to limit your | operands. Correct version:

mymatch(r'\n'.join([r'(?:(\d{10})|([a-z]{10}))', r'([a-z]{10})',
   r'(\d{10})']))

(if I understood your intention correctly)

My problem is more complex:

  ...
  File "./prepares.py", line 64, in repl
    return self.search_re.sub(self.prep_replace, text)
  File "/usr/lib/python2.1/sre.py", line 164, in _sub
    return _subn(pattern, template, string, count)[0]
  File "/usr/lib/python2.1/sre.py", line 179, in _subn
    m = c.search()
RuntimeError: maximum recursion limit exceeded

As always:

import re   # sre
re.match('^START(.+?)END$', text, re.M|re.S)

And I grew so accustomed to .expand() already :-(

I need to call GhostBusters somewhere ;-)

Sincerely yours, Roman Suzi
-- 
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at onego.ru _/
_/ Thursday, September 06, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "How many weeks are there in a light year?" _/





More information about the Python-list mailing list