Python's regular expression?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bruno at modulix

    #16
    Re: Python's regular expression?

    Davy wrote:[color=blue]
    > Hi all,
    >[/color]
    (snip)[color=blue]
    > Does Python support robust regular expression like Perl?[/color]

    Yes.
    [color=blue]
    > And Python and Perl's File content manipulation, which is better?[/color]

    From a raw perf and write-only POV, Perl clearly beats Python (regarding
    I/O, Perl is faster than C - or it least it was the last time I benched
    it on a Linux box).

    From a readability/maintenance POV, Perl is a perfect nightmare.
    [color=blue]
    > Any suggestions will be appreciated![/color]




    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in 'onurb@xiludom. gro'.split('@')])"

    Comment

    • Duncan Booth

      #17
      Re: Python's regular expression?

      Mirco Wahab wrote:
      [color=blue]
      > If you wouldn't need dictionary lookup and
      > get away with associated categories, all
      > you'd have to do would be this:
      >
      > $PATTERN = qr/
      > (blue |white |red )(?{'Colour'})
      > | (socks|tights )(?{'Garment'})
      > | (boot |shoe |trainer)(?{'Fo otwear'})
      > /x;
      >
      > $t = 'blue socks and red shoes';
      > print "$^R: $^N\n" while( $t=~/$PATTERN/g );
      >
      > What's the point of all that? IMHO, Python's
      > Regex support is quite good and useful, but
      > won't give you an edge over Perl's in the end.[/color]

      If you are desperate to collapse the code down to a single print statement
      you can do that easily in Python as well:
      [color=blue][color=green][color=darkred]
      >>> PATTERN = '''[/color][/color][/color]
      (?P<Colour>blue |white |red)
      | (?P<Garment>soc ks|tights)
      | (?P<Footwear>bo ot |shoe |trainer)
      '''[color=blue][color=green][color=darkred]
      >>> t = 'blue socks and red shoes'
      >>> print '\n'.join("%s:% s" % (match.lastgrou p,[/color][/color][/color]
      match.group(mat ch.lastgroup))
      for match in re.finditer(PAT TERN, t, re.VERBOSE))
      Colour:blue
      Garment:socks
      Colour:red
      Footwear:shoe

      Comment

      • Edward Elliott

        #18
        Re: Python's regular expression?

        bruno at modulix wrote:[color=blue]
        > From a readability/maintenance POV, Perl is a perfect nightmare.[/color]

        It's certainly true that perl lacks the the eminently readable quality of
        python. But then so do C, C++, Java, and a lot of other languages.

        And I'll grant you that perl is more susceptible to the 'executable
        line-noise' style than most other languages. This results from its
        heritage as a quick-and-dirty awk/sed type text processing language.

        But perl doesn't *have* to look that way, and not every perl program is a
        'perfect nightmare'. If you follow good practices like turning on strict
        checking, using readable variable names, avoiding $_, etc, you can produce
        pretty readable and maintainable code. It takes some discipline, but it's
        very doable. I've worked with some perl programs for over 5 years without
        any trouble. About the only thing you can't avoid are the sigils
        everywhere.

        Would I recommend perl for readable, maintainable code? No, not when better
        options like Python are available. But it can be done with some effort.

        Comment

        • Dave Hansen

          #19
          Re: Python's regular expression?

          On Wed, 10 May 2006 06:44:27 GMT in comp.lang.pytho n, Edward Elliott
          <nobody@127.0.0 .1> wrote:

          [color=blue]
          >
          >Would I recommend perl for readable, maintainable code? No, not when better
          >options like Python are available. But it can be done with some effort.[/color]

          I'm reminded of a comment made a few years ago by John Levine,
          moderator of comp.compilers. He said something like "It's clearly
          possible to write good code in C++. It's just that no one does."

          Regards,
          -=Dave

          --
          Change is inevitable, progress is not.

          Comment

          • Dave Hughes

            #20
            Re: Python's regular expression?

            Dave Hansen wrote:
            [color=blue]
            > On Wed, 10 May 2006 06:44:27 GMT in comp.lang.pytho n, Edward Elliott
            > <nobody@127.0.0 .1> wrote:
            >
            >[color=green]
            > >
            > > Would I recommend perl for readable, maintainable code? No, not
            > > when better options like Python are available. But it can be done
            > > with some effort.[/color]
            >
            > I'm reminded of a comment made a few years ago by John Levine,
            > moderator of comp.compilers. He said something like "It's clearly
            > possible to write good code in C++. It's just that no one does."[/color]

            Reminds me of the quote that used to appear on the front page of the
            ViewCVS project (seems to have gone now that they've moved and renamed
            themselves to ViewVC). Can't recall the attribution off the top of my
            head:

            "[Perl] combines the power of C with the readability of PostScript"

            Scathing ... but very funny :-)


            Dave.

            --

            Comment

            Working...