Python search and replace ip address -


i want use re.sub search , replace whatever ip address specific ip address using python example in file has

address = 192.168.1.1 or address = 1.1.1.1 or address = fe0c::1 keyword address =, rest of wildcard. want convert address = 2.2.2.2 or address = f870:2:2:2::1 . destination specific address. seemed me use re.sub simple, not mind use others

thanks

tom

try this:

#line whatever string you're trying replace re.sub("address = \s+", "address = 2.2.2.2", line) 

you call re.sub 3 parameters: re.sub(pattern, replacement, string_to_replace), pattern regular expression indicating want replace.

in our case pattern "address = \s+", saying match characters "address = " followed 1 or more non-whitespace chars. when re finds part of string matching pattern, replaces string put in replacement parameter, i.e. "address = 2.2.2.2".

(link docs on python's regular expressions)


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -