Ho to use a regular expression group reference as hash key
Fredrik Lundh
fredrik at pythonware.com
Tue Mar 12 04:27:11 EST 2002
Jason Orendorff wrote:
> > >>> re.sub( r'b\w+\b', lambda word: person.get(word,word), target )
> > 'A sample sentence with entry1 and entry2'
>
> The fourth line isn't exactly right. Should be:
>
> >>> re.sub(r'\b\w+\b', lambda word: person.get(word, word.group(0)), target)
> 'A sample sentence with entry1 and entry2'
or perhaps even:
> >>> re.sub(r'\b\w+\b', lambda m: person.get(m.group(0), m.group(0)), target)
> 'A sample sentence with value1 and value2'
('m' is a match object)
but at this point, it's probably better to replace that
lambda with a named function.
</F>
More information about the Python-list
mailing list