How to modify regular expression for ip:port?

半腔热情 提交于 2020-08-07 08:01:48

问题


I have regular expression like

match = re.findall(r'[0-9]+(?:\.[0-9]+){3}', source)

It works fine to take something like 192.168.1.1 from source string. How I can modify this regular expression for make it work with something like this:

192.168.1.1:80

Thank You for help.

P.S. Sorry for my bad english.


回答1:


This will match IP addresses with ports numbers.

match = re.findall(r'[0-9]+(?:\.[0-9]+){3}:[0-9]+', source)

If you want to make it flexible to match IP address without the ports and With the ports, you can use:

match = re.findall(r'[0-9]+(?:\.[0-9]+){3}(:[0-9]+)?', source)


来源:https://stackoverflow.com/questions/23542035/how-to-modify-regular-expression-for-ipport

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!