regexp in Python (from Perl)

Pat Pat at junk.net
Fri Oct 24 11:28:58 EDT 2008


Bruno Desthuilliers wrote:
> MRAB a écrit :
>> On Oct 19, 5:47 pm, Bruno Desthuilliers
>> <bdesth.quelquech... at free.quelquepart.fr> 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!



More information about the Python-list mailing list