regexp in Python (from Perl)

Discussion in 'Python' started by Bruno Desthuilliers, Oct 19, 2008.

  1. 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'
     
    Bruno Desthuilliers, Oct 19, 2008
    #1
    1. Advertising

  2. Bruno Desthuilliers

    Pat Guest

    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.
     
    Pat, Oct 19, 2008
    #2
    1. Advertising

  3. Bruno Desthuilliers

    MRAB Guest

    On Oct 19, 5:47 pm, 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"
    >
    > > 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").
     
    MRAB, Oct 19, 2008
    #3
  4. Bruno Desthuilliers

    Guest

    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.strip().split(".")[:3]) + ".9"

    '192.168.1.9'

    Bye,
    bearophile
     
    , Oct 20, 2008
    #4
  5. MRAB a écrit :
    > On Oct 19, 5:47 pm, Bruno Desthuilliers
    > <> wrote:
    >> 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.split('.')[0:3] + ['9'])
     
    Bruno Desthuilliers, Oct 20, 2008
    #5
  6. Bruno Desthuilliers

    Pat Guest

    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.
     
    Pat, Oct 20, 2008
    #6
  7. 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.split('.')[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" ?-)
     
    Bruno Desthuilliers, Oct 20, 2008
    #7
  8. Bruno Desthuilliers

    Pat Guest

    Bruno Desthuilliers wrote:
    > MRAB a écrit :
    >> On Oct 19, 5:47 pm, Bruno Desthuilliers
    >> <> wrote:
    >>> 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.split('.')[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!
     
    Pat, Oct 24, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Hurrell
    Replies:
    4
    Views:
    163
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    489
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    363
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    192
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    181
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page