Regular Expressions Problem

Kirk Job-Sluder kirk at eyegor.jobsluder.net
Thu Sep 9 17:32:56 EDT 2004


On 2004-09-09, Oriana <oriana.falco at thalesesec.com> wrote:
> .....I would like my code to only have one * space between lines, and
> not all that white space that I see there. I tried to use the regular
> expression: '^\*\n$^\*\n$' but that does not work. I've tried a bunch
> of things and none of them seem to work....please help!!! Thanks in
> advance, Oriana

Hrm, some suggestions.

First, you need to set the MULTILINE mode on the regular expression 
object.  You can do this with re.compile(pattern,re.MULTILINE).

Secondly, the "$" character matches just before the newline.  So it
should be '^\*$\n'.  In MULTILINE mode 

Third, the regex you have here will reduce two blank comment lines to
one.

Try this:
>>> string = """*
... *
... *
... *
... *
... *
... *
... """
>>> foo = re.compile(r'(^\*\s*\n){2,}',re.MULTILINE)
>>> foo.sub("*\n",string)
'*\n'

The blank comment line is described by (^\*\s*\n) (asterisk at the start
of a line, followed by 0 or more space characters, then a newline).
The {2,} says "match two or more of this group."

Also, I can't really overrecommend "Mastering Regular Expressions" as a
good book for regular expression users:
http://www.oreilly.com/catalog/regex/

There is also a nice python-centric regex page at:
http://www.amk.ca/python/howto/regex/


-- 
Kirk Job-Sluder
"The square-jawed homunculi of Tommy Hilfinger ads make every day an
existential holocaust."  --Scary Go Round



More information about the Python-list mailing list