Regular expressions

Michael Torrie torriem at gmail.com
Tue Nov 3 21:04:23 EST 2015


On 11/03/2015 05:33 PM, rurpy--- via Python-list wrote:
> 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.

Grep can use regular expressions (and I do so with it regularly), but
it's default mode is certainly not regular expressions, and it is still
very powerful.  I've never used regular expressions in a database query
language; until this moment I didn't know any supported such things in
their queries.  Good to know.  How you would index for regular
expressions in queries I don't know.

> 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?  

Mainly because the programming language itself often can do it just as
cleanly and just as fast (slicing, string methods, etc).  I certainly
programmed for many years without needing regular expressions in my
small projects.  In fact, REs are a bit of a pain to use in, say, C or
C++, requiring a library.  With Python they are much more readily
accessible so I use them much more.

But honestly it wasn't until college when I learned about finite state
automata that I really grasped what regular expressions were and how to
use them.

> 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. 

Maybe, maybe not.  Using Python string class methods is probably more
clear when such methods are sufficient.




More information about the Python-list mailing list