regexp in Python (from Perl)

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Mon Oct 20 04:06:21 EDT 2008


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'])



More information about the Python-list mailing list