Regular expressions
rurpy at yahoo.com
rurpy at yahoo.com
Tue Nov 3 19:33:17 EST 2015
On Monday, November 2, 2015 at 9:38:24 PM UTC-7, Michael Torrie wrote:
> On 11/02/2015 09:23 PM, rurpy--- via Python-list wrote:
> >> My completely unsolicited advice is that regular expressions shouldn't be
> >> very high on the list of things to learn. They are very useful, and very
> >> tricky and prone many problems that can and should be learned to be
> >> resolved with much simpler methods. If you really want to learn regular
> >> expressions, that's great but the problem you posed is not one for which
> >> they are the best solution. Remember simpler is better than complex.
> >
> > Regular expressions should be learned by every programmer or by anyone
> > who wants to use computers as a tool. They are a fundamental part of
> > computer science and are used in all sorts of matching and searching
> > from compilers down to your work-a-day text editor.
> >
> > Not knowing how to use them is like an auto mechanic not knowing how to
> > use a socket wrench.
>
> Not quite. Core language concepts like ifs, loops, functions,
> variables, slicing, etc are the socket wrenches of the programmer's
> toolbox. Regexs are like an electric impact socket wrench. You can do
> the same work without it, but in many cases it's slower. But you have to
> learn the other hand tools first in order to really use the electric
> driver properly (understanding torques, direction of threads, etc), lest
> you wonder why you're breaking off so many bolts with the torque of the
> impact drive.
I consider regexs more fundemental. One need not even be a programmer
to use them: consider grep, sed, a zillion editors, database query
languages, etc.
When there is a mini-language explicitly developed for describing
string patterns, why, except is very simple cases, would one not
take advantage of it? Beyond trivial operations a regex, although
terse (overly perhaps), is still likely to be more understandable
more maintainable than bunch of ad-hoc code. And the relative ease
of expressing complex patterns means one is more likely to create
more specific patterns, resulting in detecting unexpected input
earlier than with ad-hoc code.
More information about the Python-list
mailing list