Help with RegExps

Alex alex at somewhere.round.here
Wed Sep 22 12:26:59 EDT 1999


If the text you're modifiying can't have nested brackets and bracket
contents can't extend across lines, perhaps you could do something like
the following:

>>> import re
>>> def f(m):return m.group(1)+'<font color=blue>'+m.group(2)+'</font>'
... 
>>> s='some text <a href=#someRef>ref</a> # this is a comment'
>>> r=re.compile('(^[^<>]+|>[^>]+)(#.*)')
>>> print re.sub(r,f,s)
some text <a href=#someRef>ref</a> <font color=blue># this is a comment</font>
>>> 

Alex.




More information about the Python-list mailing list