Why are String Formatted Queries Considered So Magical?

Stephen Hansen me+list/python at ixokai.io
Tue Jun 29 10:11:48 EDT 2010


On 6/29/10 5:41 AM, Roy Smith wrote:
> 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.

There's nothing silly about it.

It is an exaggeration though: but it does represent a good thing to keep 
in mind.

Yes, re is a tool -- and a useful one at that. But its also a tool which 
/seems/ like an omnitool capable of tackling everything.

Regular expressions are a complicated mini-language well suited towards 
extensive use in a unix type environment where you want to embed certain 
logic of 'what to operate on' into many different commands that aren't 
languages at all -- and perl embraced it to make it perl's answer to 
text problems. Which is fine.

In Python, certainly it has its uses: many of them in fact, and in many 
it really is the best solution.

Its not just that its the right tool for some jobs and the wrong tool 
for others, or that -- as you said also -- that Python provides a rather 
rich string type which can do many common tasks natively and better, but 
that regular expressions live in the front of the mind for so many 
people coming to the language that its the first thing they even think 
of, and what should be simple becomes difficult.

So people quote that proverb. Its a good proverb. As all proverbs, its 
not perfectly applicable to all situations. But it does has an important 
lesson to it: you should generally not consider re to be the solution 
you're looking for until you are quite sure there's nothing else to 
solve the same task.

It obviously applies less to the guru's who know all about regular 
expressions and their subtleties including potential pathological behavior.

-- 

    ... Stephen Hansen
    ... Also: Ixokai
    ... Mail: me+list/python (AT) ixokai (DOT) io
    ... Blog: http://meh.ixokai.io/




More information about the Python-list mailing list