regex for multiple whitespace-only lines

MRAB google at mrabarnett.plus.com
Mon Apr 13 13:20:45 CEST 2009


Phil Mayes wrote:
> 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\re.py", line 180, in compile
>     return _compile(pattern, flags)
>   File "C:\Python25\lib\re.py", line 233, in _compile
>     raise error, v # invalid expression
> error: nothing to repeat
> 
> Does anyone know a work-round?
> 
(?m)^(?:[ \t]*\n)+



More information about the Python-list mailing list