Why are String Formatted Queries Considered So Magical?
Roy Smith
roy at panix.com
Tue Jun 29 08:41:03 EDT 2010
Nobody <nobody at nowhere.com> wrote:
> > And what about regular expressions?
>
> What about them? As the saying goes:
>
> Some people, when confronted with a problem, think
> "I know, I'll use regular expressions."
> Now they have two problems.
That's silly. RE is a good tool. Like all good tools, it is the right
tool for some jobs and the wrong tool for others.
I've noticed over the years a significant anti-RE sentiment in the
Python community. One reason, I suppose, is because Python gives you
some good string manipulation tools, i.e. split(), startswith(),
endswith(), and the 'in' operator, which cover many of the common RE use
cases. But there are still plenty of times when a RE is the best tool
and it's worth investing the effort to learn how to use them effectively.
One tool that Python gives you which makes RE a pleasure is raw strings.
Getting rid of all those extra backslashes really helps improve
readability.
Another great feature is VERBOSE. I've written some truly complicated
REs using that, and still been able to figure out what they meant the
next day :-)
More information about the Python-list
mailing list