regex for multiple whitespace-only lines

Phil Mayes olivebr at
Mon Apr 13 04:53:16 CEST 2009

I am trying to search for 1 or more empty or white-space-only lines.
Empty lines work:
 >>> re.compile('(?m)(^$){1,2}')
<_sre.SRE_Pattern object at 0x010F9218>

One line with possible whitespace works:
 >>> re.compile('(?m)(^\s*$)')
<_sre.SRE_Pattern object at 0x010F7860>

Multiple lines with possible whitespace don't:
 >>> re.compile('(?m)(^\s*$)+')
Traceback (most recent call last):
   File "<interactive input>", line 1, in <module>
   File "C:\Python25\lib\", line 180, in compile
     return _compile(pattern, flags)
   File "C:\Python25\lib\", line 233, in _compile
     raise error, v # invalid expression
error: nothing to repeat

Does anyone know a work-round?


More information about the Python-list mailing list