I wrote an informative blog article on this topic: How to extract IPv4 and IPv6 IP addresses from plain text using Regex .
The article provides a detailed guide to the most common different patterns for IP addresses, which often need to be extracted and isolated from plain text using regular expressions.
This guide is based on the CodVerter IP Extractor source code tool to handle extraction and discovery of IP addresses as needed.
If you want to check and capture the IPv4 address, this template can do the job:
\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.]){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b
or for checking and capturing an IPv4 address with a prefix (slash):
\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.]){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?/[0-9]{1,2})\b
or to capture a subnet mask or mask:
(255|254|252|248|240|224|192|128|0)[.](255|254|252|248|240|224|192|128|0)[.](255|254|252|248|240|224|192|128|0)[.](255|254|252|248|240|224|192|128|0)
or filter out the subnet mask, the address that you do this with a regular expression of negative preview :
\b((?!(255|254|252|248|240|224|192|128|0)[.](255|254|252|248|240|224|192|128|0)[.](255|254|252|248|240|224|192|128|0)[.](255|254|252|248|240|224|192|128|0)))(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.]){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b
To verify IPv6, you can follow the link to the article that I added at the top of this answer.
Here is an example to capture all common patterns (taken from the CodVerter IP Extractor help example):

If you want, you can check the IPv4 regex here .
jonathana Jan 06 '19 at 20:06 2019-01-06 20:06
source share