[Tutor] regexp help

Botykai Zsolt zsolt.botykai at online.hu
Fri Sep 24 15:42:51 CEST 2004

thanx, workx as it should have.


csütörtök 23 szeptember 2004 17:26 dátummal Kent Johnson ezt írta:
> OK. I suggest you use re.sub() on the match data to remove the blank lines.
> So you would use one re to get the data and another one to remove the blank
> lines. For example:
>  >>> import re
>  >>> data = '   \n   \nhere\nis\n  \nsome data\n\nwith\nblank
> lines   \n   \n   '
>  >>> rx = re.compile(r'(^|\n)\s*(\n|$)')
>  >>> rx.sub(r'\1', data)
> 'here\nis\nsome data\nwith\nblank lines   \n'
>  >>> rx.sub(r'\1', '\n\n\ntest\n\n\n')
> 'test\n'
> The re to match blank lines is a little tricky because you want to match
> '   \n' - a blank line at the beginning
> '\n  \n' - a blank line in the middle
> '   ' - a blank line at the end
> The solution I used was to allow either beginning of string ^ or newline \n
> before the blank, and either newline or end of string $ at the end. Then I
> replace by whatever matched at the beginning so the blank line at start is
> not replaced by a newline itself.
> If your data uses \r\n for newlines you will have to modify the regex a
> little bit.


Opinions, conclusions and other information in this message that do not
relate to the official business of Online Business Technologies Corp.
shall be understood as neither given nor endorsed by it. In such cases
Online Business Technologies Corp. will not bear the responsibility of
If you have received this communication in error, please notify the
system manager immediately by responding to this email and then delete
it from your system.

A levelben foglalt, nem az Online Rt. hivatalos uzletmenetevel kapcsolatos
velemenyek vagy mas informaciok vonatkozasaban az Online Rt. nem vallal
Amennyiben a level valamely hiba folytan jutott Onhoz, kerjuk, hogy
valaszlevelben azonnal ertesitse a rendszer uzemeltetojet, majd torolje ki
a levelet rendszerebol!

More information about the Tutor mailing list