Help with RegExps
Chris...
sca at isogmbh.de
Mon Sep 27 07:59:46 EDT 1999
Hello Alex...
Thanks for your solution!
I wasn't aware of a function as substitution, that solved another problem.
Btw. if I want to add additional parameters, e.g. the color in your function f
below
is this way the only way (I'm missing partial application, higher-order functions
as in functional languages):
def createFun(color):
return (lambda m, color = color: m.group(1) + '<font color=' + color + '>' \
+ m.group(2) + '</font>')
and somewhere in your code:
f = createFun('blue')
Is this right? Looks a little bit clumsy, but I don't know of a better
solution :-(
Alex wrote:
> 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>
> >>>
There are some problems with that pattern:
# at beginning of line isn't matched, two or more #'s aren't matches correctly.
I
think, there's no solution for my problem except parsing the lines.
Furthermore,
I read the file using read, not readlines, because I have to do multiline
matchings/substitutions.
Thank you!
bye
Chris...
More information about the Python-list
mailing list