regexp in Python (from Perl)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pat

    regexp in Python (from Perl)

    I have a regexp in Perl that converts the last digit of an ip address to
    '9'. This is a very particular case so I don't want to go off on a
    tangent of IP octets.

    ( my $s = $str ) =~ s/((\d+\.){3})\d+/${1}9/ ;

    While I can do this in Python which accomplishes the same thing:

    ip = ip[ :-1 ]
    ip =+ '9'

    I'm more interested, for my own edification in non-trivial cases, in how
    one would convert the Perl RE to a Python RE that use groups. I am
    somewhat familiar using the group method from the re package but I
    wanted to know if there was a one-line solution.

    Thank you.
  • MRAB

    #2
    Re: regexp in Python (from Perl)

    On Oct 19, 5:47 pm, Bruno Desthuilliers
    <bdesth.quelque ch...@free.quel quepart.frwrote :
    Pat a écrit :
    >
    I have a regexp in Perl that converts the last digit of an ip address to
     '9'.  This is a very particular case so I don't want to go off on a
    tangent of IP octets.
    >
     ( my $s = $str ) =~ s/((\d+\.){3})\d+/${1}9/ ;
    >
    While I can do this in Python which accomplishes the same thing:
    >
    ip = ip[ :-1 ]
    ip =+ '9'
    >
    or:
    >
    ip = ip[:-1]+"9"
    >
    I'm more interested, for my own edification in non-trivial cases, in how
    one would convert the Perl RE to a Python RE that use groups.  I am
    somewhat familiar using the group method from the re package but I
    wanted to know if there was a one-line solution.
    >
    Is that what you want ?
    >
     >>re.sub(r'^((( \d+)\.){3})\d+$ ', "\g<1>9", "192.168.1. 1")
    '192.168.1.9'
    >
    >re.sub(r'^(((\ d+)\.){3})\d+$' , "\g<1>9", "192.168.1.100" )
    >
    '192.168.1.9'
    The regular expression changes the last sequence of digits to
    "9" ("192.168.1.100 " ="192.168.1. 9") but the other code replaces the
    last digit ("192.168.1.100 " ="192.168.1.109 ").

    Comment

    • bearophileHUGS@lycos.com

      #3
      Re: regexp in Python (from Perl)

      MRAB:
      The regular expression changes the last sequence of digits to
      "9" ("192.168.1.100 " ="192.168.1. 9") but the other code replaces the
      last digit ("192.168.1.100 " ="192.168.1.109 ").
      Uhmm, this is a possible alternative:
      >>s = " 192.168.1.100 "
      >>".".join(s.st rip().split("." )[:3]) + ".9"
      '192.168.1.9'

      Bye,
      bearophile

      Comment

      • Bruno Desthuilliers

        #4
        Re: regexp in Python (from Perl)

        MRAB a écrit :
        On Oct 19, 5:47 pm, Bruno Desthuilliers
        <bdesth.quelque ch...@free.quel quepart.frwrote :
        >Pat a écrit :
        (snip)
        >>ip = ip[ :-1 ]
        >>ip =+ '9'
        >or:
        >>
        >ip = ip[:-1]+"9"
        >>
        (snip)
        > >>re.sub(r'^((( \d+)\.){3})\d+$ ', "\g<1>9", "192.168.1. 1")
        >'192.168.1.9 '
        >>
        >>>>re.sub(r'^( ((\d+)\.){3})\d +$', "\g<1>9", "192.168.1.100" )
        >'192.168.1.9 '
        >
        The regular expression changes the last sequence of digits to
        "9" ("192.168.1.100 " ="192.168.1. 9") but the other code replaces the
        last digit ("192.168.1.100 " ="192.168.1.109 ").
        Mmm - yes, true.

        ip = ".".join(ip.spl it('.')[0:3] + ['9'])

        Comment

        • Pat

          #5
          Re: regexp in Python (from Perl)

          Bruno Desthuilliers wrote:
          Pat a écrit :
          >I have a regexp in Perl that converts the last digit of an ip address
          >to '9'. This is a very particular case so I don't want to go off on
          >a tangent of IP octets.
          >>
          > ( my $s = $str ) =~ s/((\d+\.){3})\d+/${1}9/ ;
          >>
          >While I can do this in Python which accomplishes the same thing:
          >>
          >ip = ip[ :-1 ]
          >ip =+ '9'
          >
          or:
          >
          ip = ip[:-1]+"9"
          Yes! That's exactly what I was looking for.
          >
          >
          >I'm more interested, for my own edification in non-trivial cases, in
          >how one would convert the Perl RE to a Python RE that use groups. I
          >am somewhat familiar using the group method from the re package but I
          >wanted to know if there was a one-line solution.
          >
          Is that what you want ?
          >
          >>re.sub(r'^((( \d+)\.){3})\d+$ ', "\g<1>9", "192.168.1. 1")
          '192.168.1.9'
          >
          >>>re.sub(r'^(( (\d+)\.){3})\d+ $', "\g<1>9", "192.168.1.100" )
          '192.168.1.9'
          >
          >
          Ah-hah! That's how one uses groups. It's beautiful. I couldn't find
          that in my books. Thank you very, very much!

          At first, I thought that using RE's in Python was going to be more
          difficult than Perl. A lot of my Perl code makes heavy use of RE
          searching and substitution.

          I will never, ever write another line of Perl code as long as I live.

          Comment

          • Bruno Desthuilliers

            #6
            Re: regexp in Python (from Perl)

            Pat a écrit :
            Bruno Desthuilliers wrote:
            >Pat a écrit :
            >>I have a regexp in Perl that converts the last digit of an ip address
            >>to '9'. This is a very particular case so I don't want to go off on
            >>a tangent of IP octets.
            >>>
            >> ( my $s = $str ) =~ s/((\d+\.){3})\d+/${1}9/ ;
            >>>
            >>While I can do this in Python which accomplishes the same thing:
            >>>
            >>ip = ip[ :-1 ]
            >>ip =+ '9'
            >>
            >or:
            >>
            >ip = ip[:-1]+"9"
            >
            Yes! That's exactly what I was looking for.
            Perhaps not - cf MRAB's post earlier in this thread and bearophile's answer.

            While this was a straightforward one-liner version of your own code, it
            doesn't behave like the re version : this one only replace the last
            _character_, while the re version replaces the last _group_. If you want
            the re version's behaviour, use this instead:

            ip = ".".join(ip.spl it('.')[0:3] + ['9'])

            >>
            >>
            >>I'm more interested, for my own edification in non-trivial cases, in
            >>how one would convert the Perl RE to a Python RE that use groups. I
            >>am somewhat familiar using the group method from the re package but I
            >>wanted to know if there was a one-line solution.
            >>
            >Is that what you want ?
            >>
            > >>re.sub(r'^((( \d+)\.){3})\d+$ ', "\g<1>9", "192.168.1. 1")
            >'192.168.1.9 '
            >>
            >>>>re.sub(r'^( ((\d+)\.){3})\d +$', "\g<1>9", "192.168.1.100" )
            >'192.168.1.9 '
            >>
            >>
            >
            Ah-hah! That's how one uses groups. It's beautiful. I couldn't find
            that in my books.
            Did you look at the FineManual(tm) ?-)
            >
            At first, I thought that using RE's in Python was going to be more
            difficult than Perl. A lot of my Perl code makes heavy use of RE
            searching and substitution.
            Well... Python's re module makes for a bit more verbose code, and I'm
            not sure all of the perl's regexps features are implemented. But that's
            still quite enough to shoot yourself in the foot IMHO !-)

            Now while regexps are a must-have in a programmer's toolbox, you can
            already do quite a few things just using slicing and string methods. I
            highly recommend you read the relevant doc.
            I will never, ever write another line of Perl code as long as I live.
            Hmmm... Never say "never again" ?-)

            Comment

            • Pat

              #7
              Re: regexp in Python (from Perl)

              Bruno Desthuilliers wrote:
              MRAB a écrit :
              >On Oct 19, 5:47 pm, Bruno Desthuilliers
              ><bdesth.quelqu ech...@free.que lquepart.frwrot e:
              >>Pat a écrit :
              (snip)
              >>>ip = ip[ :-1 ]
              >>>ip =+ '9'
              >>or:
              >>>
              >>ip = ip[:-1]+"9"
              >>>
              (snip)
              >> >>re.sub(r'^((( \d+)\.){3})\d+$ ', "\g<1>9", "192.168.1. 1")
              >>'192.168.1. 9'
              >>>
              >>>>>re.sub(r'^ (((\d+)\.){3})\ d+$', "\g<1>9", "192.168.1.100" )
              >>'192.168.1. 9'
              >>
              >The regular expression changes the last sequence of digits to
              >"9" ("192.168.1.100 " ="192.168.1. 9") but the other code replaces the
              >last digit ("192.168.1.100 " ="192.168.1.109 ").
              >
              Mmm - yes, true.
              >
              ip = ".".join(ip.spl it('.')[0:3] + ['9'])
              As I first stated, in my very particular case, I knew that the last
              octet was always going to be a single digit.

              But I did learn a lot from everyone else's posts for the more generic
              cases. thx!

              Comment

              Working...