[Tutor] Regex's and "best practice"
dyoo at hkn.eecs.berkeley.edu
Mon Nov 24 02:59:30 EST 2003
> > I would call it a fair solution. Perl is very hard to beat on the
> > regex front simply because regular expressions are in the language,
> > not a module.
> It seems that they could be a bit less clunky. It's counter-intuitive
> that match() and search() allow including flags, but to use flags with
> sub() you have to compile() the regex first.
It is possible to include flags as part of the pattern. Here's one
example that does a case-insensitive substitution:
>>> re.sub("Z(?i)", " ", "ThisziszaZtest")
'This is a test'
This is similar to the Perl code:
my $msg = "ThisziszaZtest";
$msg =~ s/Z/ /ig;
and most of the other familiar Perl flags should work with Python's
regular expressions too. We can find information about the rest of the
(?...) flags here:
> > For a sysadmin, I think the win with python is the ease of writing
> > reusable modules that are tailored for your system. I presumme you
> > understand Python's if __name__ == '__main__':
> I've only been doing this for a few days, so I haven't run across this.
> Sounds useful, though, and I'll keep it in mind.
Since you've done some Perl programming already, you might find this
It's Mark Pilgrim's Python tutorial, geared toward experienced
programmers; I think you'll enjoy it.
Best of wishes to you!
More information about the Tutor