Python vs. Perl, which is better to learn?

Ian Bicking ianb at colorstudy.com
Wed May 1 02:03:35 EDT 2002


On Wed, 2002-05-01 at 00:02, Christopher Browne wrote:
> Mind you, I have found that the "More Pythonic Way" of constructing
> complex regexes by building components, like the following, to have
> merit:
> 
>   digit = '[0-9]'
>   date = '\(' + digit + digit + digit + digit + '/' + digit + digit + '/'
>   date = date + digit + digit + '\)'
> 
> By building the regex out of something with named components, I don't
> have the horridness of line noise like:
>    if ($line =~ /.*\s+\d+.\d+\s+\d+.\d+\s+.\d+.\d+/) {

Sounds like you want SNOBOL-like regexes, perhaps?  I feel like someone
implemented something along those lines for Python, probably more as an
experiment than for practical reasons.  I've also seen Lispy regex
engines where you really feel like you are creating a state machine
(which you more or less are anyway -- it just feels more like it).  I
can't remember them well, but I feel like you express it like 
  '("(" digit digit digit digit "/" digit digit ...) 
probably with other operators, so you wouldn't have to write "digit
digit digit digit" and such.

These of course could also be implemented in Python -- though the Lisp
syntax may actually be an advantage here (or at least the separation of
strings and symbols).  Since Python doesn't give any special preference
to Perl-like regexes, it's just as possible to have a re2 library, with
re2.compile([regex expressed as a list...]), where the compiled objects
otherwise act just like re compiled objects, and are thus
interchangeable.  Considering how Dark and Mysterious regexes are to
novices, this might be well worth it.

  Ian







More information about the Python-list mailing list