Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > regexp in Python (from Perl)

Reply
Thread Tools

regexp in Python (from Perl)

 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      10-19-2008
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'


 
Reply With Quote
 
 
 
 
Pat
Guest
Posts: n/a
 
      10-19-2008
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.
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      10-19-2008
On Oct 19, 5:47*pm, Bruno Desthuilliers
<(E-Mail Removed)> 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").
 
Reply With Quote
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      10-19-2008
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
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      10-20-2008
MRAB a écrit :
> On Oct 19, 5:47 pm, Bruno Desthuilliers
> <(E-Mail Removed)> 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'])
 
Reply With Quote
 
Pat
Guest
Posts: n/a
 
      10-20-2008
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.
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      10-20-2008
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" ?-)
 
Reply With Quote
 
Pat
Guest
Posts: n/a
 
      10-24-2008
Bruno Desthuilliers wrote:
> MRAB a écrit :
>> On Oct 19, 5:47 pm, Bruno Desthuilliers
>> <(E-Mail Removed)> 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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
new RegExp().test() or just RegExp().test() Matìj Cepl Javascript 3 11-24-2009 02:41 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Programmatically turning a Regexp into an anchored Regexp Greg Hurrell Ruby 4 02-14-2007 06:56 PM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:59 PM



Advertisments