scanf style parsing

Ralph Corderoy ralph at inputplus.demon.co.uk
Sat Sep 29 07:09:03 EDT 2001


Hi Tim,

> > For Perl hackers it is easy to figure out regexp, but for us old
> > C/C++ types, it's *tough*
> 
> It's not usually easy to learn regexps, no matter what your
> background.  I come from C/C++ roots (Turbo C++ 3.0) and TRS-80 BASIC
> before that, and I certainly had no idea what regex's were really for
> until I looked at Perl.
> 
> I struggled with regex's for months. I even had to take some time
> away from Perl and regex's to calm down and not be so intimidated.
> Many Pythonistas I've heard in this ng had a lot of difficulty with
> regular expressions, and with *good reason*.

An easy, sure fire way for any programmer to learn regex concepts is to
read Kernighan and Plauger's _Software Tools_ where, amongst many other
interesting topics, they implement a regex pattern matcher and preceed
to use it in their versions of grep, ed, etc.

This gives you an immediate understanding of how conceptually simple
the implementation is and also makes issues like greediness, or
pathological patterns, simple to understand.  

> Regex's are useful and powerful.  But they're also very easy to
> abuse.  I've actually seen the following Perl code:
> 
>     if ($filename =~ /\.txt$/) { ... }
> 
> ...
>
> or, much more preferably:
> 
>     if filename[-4:] == '.txt':

Overall, the Perl code's better.  It didn't have to hard-code the
length of the string.

Cheers,


Ralph.

PS.  Every programmer should read _Software Tools_ anyway.




More information about the Python-list mailing list