Regular expression – how to modify IP: PORT regular expressions?

I have normal expressions

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

It can get content similar to 192.168.1.1 from the source string.
How do I modify this regular expression to make It applies to things like this:

192.168.1.1:80

Thank you for your help.

P.S. Sorry, I English is not good.

This will match the IP address with the port number.

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

If you want to match the IP address flexibly without ports and ports, you can use:

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

< p>I have a normal expression

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

It can get something like 192.168.1.1 from the source string.
How do I modify this regular expression to make it suitable for things like this:

p>

192.168.1.1:80

Thank you for your help.

Attachment: Sorry, my English is not good.

This will match the IP address with the port number.

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

If you want to be flexible without ports and ports To match the IP address, you can use:

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

Leave a Comment

Your email address will not be published.