Help, Search and Substitute

Simon Brunning SBrunning at trisystems.co.uk
Fri Nov 3 04:27:27 EST 2000


> From:	andy47 at my-deja.com [SMTP:andy47 at my-deja.com]
> > # based on re-example-5.py
> > # from (the eff-bot guide to) The Python Standard Library
> > # http://www.pythonware.com/people/fredrik/librarybook.htm
> >
> > import re
> > import string
> >
> > symbol_map = { "foo": "FOO", "bar": "BAR" }
> >
> > def symbol_replace(match, get=symbol_map.get):
> >     return get(match.group(1), "")
> >
> > symbol_pattern = re.compile(
> >     "\{(" + string.join(map(re.escape, symbol_map.keys()), "|") + ")
> \}"
> >     )
> >
> > print symbol_pattern.sub(symbol_replace, "{foo}fie{bar}")
> >
> > </F>
> 
> Folks,
> 
> I'm probably being really dumb here. I can see that the above works,
> and I've got a vague idea of how it works but the detail is escaping
> me. Can anyone explain how it works in words of one syllable to us
> newbies? In particular, I'm a little lost as to how the function
> symbol_replace works.
 
/F sent me a similar snippet a while back, in response to a similar query,
and it took me a little while to understand it. Here goes...

Firstly, symbol_map is a dictionary containing the 'from' & 'to' values.
(Let's ignore the symbol_replace function until later...)

/F then builds a re, basically consisting of all the 'from' values, joined
by '|'s. (Note the use if re.escape - this escapes any characters having
special meaning within regular expressions, such as '|' or '['. Have a play
with it interactively.)

/F then calls symbol_pattern's sub method. This takes two arguments, the
second being the string to be changed. The first argument of the sub method
can be one of two things - it can simply be a string, in which case all
substrings matching the re have that string substituted. Or, if you need
something a little more complex, as you do, it can be a function...

This is where the symbol_replace function comes in. It has been passed the
match value, i.e. the substring that the re matched, so it can use that as a
key value to look up the 'to' value in the symbol_map dictionary. It returns
this to the sub method, which uses it to do the replacement.

(I don't know why /F made 'get' a defaulted argument of the function - but
then he's *far* cleverer that I, so there was *bound* to be something that I
didn't understand!)

Cheers,
Simon Brunning
TriSystems Ltd.
sbrunning at trisystems.co.uk
When I was a kid, I used to pray every night for a new bicycle. Then I
realised that the Lord, in his wisdom, didn't work that way. So I just stole
one and asked him to forgive me. - Emo Philips 




-----------------------------------------------------------------------
The information in this email is confidential and may be legally privileged.
It is intended solely for the addressee. Access to this email by anyone else
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
accept liability for statements made which are clearly the senders own.




More information about the Python-list mailing list