Why are String Formatted Queries Considered So Magical?
Stephen Hansen
me+list/python at ixokai.io
Thu Jul 1 03:19:09 EDT 2010
On 6/30/10 11:58 PM, Jorgen Grahn wrote:
> On Wed, 2010-06-30, Steven D'Aprano wrote:
>> On Wed, 30 Jun 2010 14:14:38 +0000, Jorgen Grahn wrote:
>>> On Tue, 2010-06-29, Stephen Hansen wrote:
>>>>
>>>> There's nothing silly about it.
>>>>
>>>> It is an exaggeration though: but it does represent a good thing to
>>>> keep in mind.
>>>
>>> Not an exaggeration: it's an absolute. It literally says that any time
>>> you try to solve a problem with a regex, (A) it won't solve the problem
>>> and (B) it will in itself become a problem. And it doesn't tell you
>>> why: you're supposed to accept or reject this without thinking.
>>
>> It's a *two sentence* summary, not a reasoned and nuanced essay on the
>> pros and cons for REs.
>
> Well, perhaps you cannot say anything useful about REs in general in
> two sentences, and should use either more words, or not say anything
> at all.
>
> The way it was used in the quoted text above is one example of what I
> mean. (Unless other details have been trimmed -- I can't check right
> now.) If he meant to say "REs aren't really a good solution for this
> kind of problem, even though they look tempting", then he should have
> said that.
The way it is used above (Even with more stripping) is exactly where it
is legitimate.
Regular expressions are a powerful tool.
The use of a powerful tool when a simple tool is available that achieves
the same end is inappropriate, because power *always* has a cost.
The entire point of the quote is that when you look at a problem, you
should *begin* from the position that a complex, powerful tool is not
what you need to solve it.
You should always begin from a position that a simple tool will suffice
to do what you need.
The quote does not deny the power of regular expressions; it challenges
widely held assumption and belief that comes from *somewhere* that they
are the best way to approach any problem that is text related.
Does it come off as negative towards regular expressions? Certainly. But
not because of any fault of re's on their own, but because there is this
widespread perception that they are the swiss army knife that can solve
any problem by just flicking out the right little blade.
Its about redefining perception.
Regular expressions are not the go-to solution for anything to do with
text. Regular expressions are the tool you reach for when nothing else
will work.
Its not your first step; its your last (or, at least, one that happens
way later then most people come around expecting it to be).
--
... 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