From: Ethan Furman on
Terry Reedy wrote:
> On 6/30/2010 8:22 AM, Nobody wrote:
>
>>> I've noticed over the years a significant anti-RE sentiment in the
>>> Python community.
>>
>> IMHO, the sentiment isn't so much against REs per se, but against
>> excessive or inappropriate use. Apart from making it easy to write
>> illegible code, they also make it easy to write code that "mostly sort-of
>> works" but somewhat harder to write code which is actually correct.
>>
>> It doesn't help that questions on REs often start out by stating a
>> problem
>> for which REs are inappropriate, e.g. parsing a context-free (or higher)
>> language, and in the same sentence indicate the the poster is already
>> predisposed to using REs.
>
> They also often start with a problem that is 'sub-relational-grammar'
> and easily solved with string methods, and again the OP proposes to use
> the overkill of REs. In other words, people ask "How do I do this with
> an RE" rather than "What tool should I use for this, and how".
>
> If people asked "How do I push a pin into a corkboard with a (standard)
> hammer" or "How do I break up a concrete sidewalk with a (standard)
> hammer), it would not be 'anti-hammer sentiment' to suggest another
> tool, like pliers or a jackhammer.

I took the time to learn REs about a year ago. It was well worth it,
even though I've only used REs a handful of times since, because when
you need them there is no good substitute. But when you don't, there
are plenty. ;)

~Ethan~

From: Steven D'Aprano on
On Wed, 30 Jun 2010 14:14:38 +0000, Jorgen Grahn wrote:

> On Tue, 2010-06-29, Stephen Hansen wrote:
>> On 6/29/10 5:41 AM, Roy Smith wrote:
>>> Nobody<nobody(a)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.
>
> 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.

Sheesh, I can just imagine you as a child, arguing with your teacher on
being told not to run with scissors -- "but teacher, there may be
circumstances where running with scissors is the right thing to do, you
are guilty of over-simplifying a complex topic into a single simplified
sound-byte, instead of providing a detailed, rich heuristic for analysing
each and every situation in full before making the decision whether or
not to run with scissors".

If you look at the quote carefully, instead of making a knee-jerk
reaction, you will see that it is *literally* correct. Given some
problem, having decided to solve it with a regex, you DO have two
problems:

(1) Merely making the decision "use REs" doesn't actually solve the
original problem, any more than "use a hammer" solves the problem of "how
do I build a table?". You've decided on an approach and a tool, but your
original problem still applies.

(2) AND you now have the additional problem of dealing with regular
expressions, which are notoriously hard to write, harder to debug,
difficult to maintain, often slow, incapable of solving certain common
problems (such as parsing nested parentheses).

So it might be a short, simplified quip, but it *is* literally correct.



> How can that be a good thing to keep in mind?

Because many people consider REs to be some sort of panacea for solving
every text-based problem, and it's a good thing to open their eyes.



--
Steven
From: Jorgen Grahn on
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:
>>> On 6/29/10 5:41 AM, Roy Smith wrote:
>>>> Nobody<nobody(a)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.
>>
>> 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.

> Sheesh, I can just imagine you as a child, arguing with your teacher on
> being told not to run with scissors -- "but teacher, there may be
> circumstances where running with scissors is the right thing to do, you
> are guilty of over-simplifying a complex topic into a single simplified
> sound-byte, instead of providing a detailed, rich heuristic for analysing
> each and every situation in full before making the decision whether or
> not to run with scissors".

When I was a child I expected that kind of argumentation from adults.
I expect something more as an adult.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
From: Stephen Hansen on
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/

From: Jean-Michel Pichavant on
Stephen Hansen wrote:
> 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).
>

Guys, this dogmatic discussion already took place in this list. Why
start again ?
Re is part of the python standard library, for some purpose I guess.

JM