Preface:
If you want to check whether a certain string represents a valid IPv4 address, try one of these examples from Regular Expressions Cookbook.
(底下範例代碼使用 Groovy 語言)
Simple regex to check for an IP address:
A version 4 IP address is usually written in the form 255.255.255.255, where each of the four numbers must be between 0 and 255. Matching such IP addresses with a regular expression is very straightforward. The simple regexes use [0-9]{1,3} to match each of the four blocks of digits in the IP address. These actually allow numbers from 0 to 999 rather than 0 to 255. The simple regexes are more efficient when you already know your input will contain only valid IP addresses, and you only need to separate the IP addresses from the other stuff.
- def hosts = ["140.112.31.68", "999.999.999.999", "abc.com.tw"]
- hosts.each{ host->
- if (host =~ /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/)
- {
- printf "'%s' matches IPv4 format!\n", host
- }
- else
- {
- printf "'%s' does't match IPv4 format!\n", host
- }
- }
Accurate regex to check for an IP address:
The accurate regexes use 25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]? to match each of the four numbers in the IP address. This regex accurately matches a number in the range 0 to 255, with one optional leading zero for numbers between 10 and 99, and two optional leading zeros for numbers between 0 and 9. 25[0-5] matches 250 through 255, 2[0-4][0-9] matches 200 to 249, and [01]?[0-9][0-9]? takes care of 0 to 199, including the optional leading zeros.
- def hosts = ["140.112.31.68", "999.999.999.999", "abc.com.tw"]
- def IPv4Ptn = /^(?:(?: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]?)$/
- hosts.each{ host->
- if (host =~ IPv4Ptn)
- {
- printf "'%s' matches IPv4 format!\n", host
- }
- else
- {
- printf "'%s' does't match IPv4 format!\n", host
- }
- }
Regex to extract IP addresses from longer text:
If you want to check whether a string is a valid IP address in its entirety, use one of the regexes that begin with a caret and end with a dollar. These are the start-of-string and end-of-string anchors. If you want to find IP addresses within longer text, use one of the regexes that begin and end with the word boundaries \b:
- def IPv4Ptn = /\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/
- def IPv4Ptn = /\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]?)/
Regex that captures the four parts of the IP address:
In order to capture the four numbers separately, instead of using the trick of repeating a group three times, they have four capturing groups, one for each number. Spelling things out this way is the only way we can separately capture all four numbers in the IP address.
- def hosts = ["140.112.31.68", "999.999.999.999", "abc.com.tw", "IPv4 123.24.122.89"]
- def IPv4Ptn = /\b([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\b/
- hosts.each{ host->
- match = host =~ IPv4Ptn
- if (match.find())
- {
- printf "'%s' matches IPv4 format!\n", match[0][0]
- printf "\tPart1=%s\n", match[0][1]
- printf "\tPart2=%s\n", match[0][2]
- printf "\tPart3=%s\n", match[0][3]
- printf "\tPart4=%s\n", match[0][4]
- println ""
- }
- else
- {
- printf "'%s' does't match IPv4 format!\n", host
- }
- }
- def IPv4Ptn = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
沒有留言:
張貼留言