matching one time through a loop

Kragen Sitaker kragen at pobox.com
Sat Jun 1 02:11:55 EDT 2002


vvainio at tp.spt.fi (Ville Vainio) writes:
> BTW, how does perl do re.findall()? Or re.sub using function return
> values to dynamically determine the replacement? I can't remember I
> ever used such features with perl.

re.findall():

my $string = 'a moon upon a lunar tune aroon';
my @list = ($string =~ /\w+n/g);
print map { "$_\n" } @list;

I'm afraid I always have to look up what // returns every time I use it.


re.sub using function return values is approximately the /e modifier,
but without the need to wrap a 'lambda:' around the replacement:

my %dict = qw(a the moon rock upon above lunar solar tune song aroon boon);
my $string = 'a moon upon a lunar tune aroon';
$string =~ s/\w+/$dict{$&}/ge;
print $string, "\n";

Compare Python:
import re
mydict = {'a': 'the', 'moon': 'rock', 'upon': 'above', 'lunar': 'solar',
          'tune': 'song', 'aroon': 'boon'}
astring = 'a moon upon a lunar tune aroon'
astring = re.sub(r'\w+', lambda mo: mydict[mo.group(0)], astring)
print astring

Not only is the Python harder to read (to me), but it took me four
tries to get it to work, because I made the following three mistakes:
- I got the order of re.sub arguments wrong (and got a *very* unhelpful
  traceback from line 177 in sre.py)
- I called the lambda argument 'aword' in one place and 'word' in the other
- I thought the lambda argument was the matched string, not the match object
  (if it had been the matched string, I could have written mydict.get instead
  of the lambda expression)

The Perl version worked the first time; you will note that none of the
three Python mistakes have a reasonable analogue in the Perl version.

> My experience is that Python regexps are better when you go beyond
> non-trivial, with clear group & matchobject semantics

I wholeheartedly agree.  But the vast majority of my regexps are
trivial.





More information about the Python-list mailing list