user2386137
user2386137

Reputation: 71

I want to isolate the IP and the port from a string in a python script

I have a script which will read in information, and process it.

I have the script so that it reads in the information.

For example:

RawInfo[0] = "192.168.100.254:8081"

I want to be able to isolate the port number, and isolate the IP address, and store them in two separate variables, so the end result of my script is

IP_from_RawInfo = "192.168.100.254"
Port_from_rawInfo = "8081"

I know regex can do this somehow but I don't really know how to use it! Can somebody give me a hand? Thanks!

Upvotes: 1

Views: 1804

Answers (2)

TerryA
TerryA

Reputation: 60024

Even with regex it's dead easy ;p

>>> import re
>>> ip, other = re.split(':', s)
>>> print ip
192.168.100.254
>>> print other
8081

But as @zhangyangyu showed, you can simply use .split(':')

Upvotes: 1

zhangyangyu
zhangyangyu

Reputation: 8620

>>> basis = "192.168.100.254:8081"
>>> basis.split(':')
['192.168.100.254', '8081']
>>> 

Upvotes: 3

Related Questions