regex for multiple whitespace-only lines

Mark Tolonen metolone+gmane at gmail.com
Sun Apr 12 23:45:42 EDT 2009


"Phil Mayes" <olivebr at olivebr.com> wrote in message 
news:5.2.1.1.0.20090412194731.013d02e0 at olivebr.com...
>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?

PythonWin 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit 
(Intel)] on win32.
Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' for 
further copyright information.
>>> import re
>>> data='hello\n\n\n\n\ngoodbye\n' # 4 blank lines
>>> re.search('(?m)^\s*\n',data).group(0)
'\n\n\n\n'

Note that $ in MULTILINE mode matches just before a newline or the end of a 
string, so it won't include the last newline.  \s* is greedy and will match 
multiple lines.

-Mark





More information about the Python-list mailing list