我想从python脚本中的字符串中隔离IP和端口(I want to isolate the IP and the port from a string in a python script)

我有一个脚本,它将读取信息并处理它。

我有脚本,以便它读取信息。

例如:

RawInfo[0] = "192.168.100.254:8081"

我希望能够隔离端口号,并隔离IP地址,并将它们存储在两个单独的变量中,因此我的脚本的最终结果是

IP_from_RawInfo = "192.168.100.254" Port_from_rawInfo = "8081"

我知道正则表达式可以以某种方式做到这一点,但我真的不知道如何使用它! 有人可以帮我一把吗? 谢谢!

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!

最满意答案

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

更多推荐