程式扎記: [ Java 文章收集 ] How to Match IPv4 Addresses with Regular Expressions

標籤

2014年3月21日 星期五

[ Java 文章收集 ] How to Match IPv4 Addresses with Regular Expressions

來源自 這裡 
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
  1. def hosts = ["140.112.31.68""999.999.999.999""abc.com.tw"]  
  2.   
  3. hosts.each{ host->  
  4.     if (host =~ /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/)  
  5.     {  
  6.         printf "'%s' matches IPv4 format!\n", host  
  7.     }  
  8.     else  
  9.     {  
  10.         printf "'%s' does't match IPv4 format!\n", host  
  11.     }  
  12. }  
輸出結果: 
'140.112.31.68' matches IPv4 format!
'999.999.999.999' matches IPv4 format!
 // False Positive!
'abc.com.tw' does't match IPv4 format!

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. 
  1. def hosts = ["140.112.31.68""999.999.999.999""abc.com.tw"]  
  2.   
  3. 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]?)$/  
  4. hosts.each{ host->  
  5.     if (host =~ IPv4Ptn)  
  6.     {  
  7.         printf "'%s' matches IPv4 format!\n", host  
  8.     }  
  9.     else  
  10.     {  
  11.         printf "'%s' does't match IPv4 format!\n", host  
  12.     }  
  13. }  
執行結果: 
'140.112.31.68' matches IPv4 format!
'999.999.999.999' does't match IPv4 format!
 // 可以偵測出來
'abc.com.tw' does't match IPv4 format!

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
  1. def IPv4Ptn = /\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/  
如果是 Accurate 版本: 
  1. 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. 
  1. def hosts = ["140.112.31.68""999.999.999.999""abc.com.tw""IPv4 123.24.122.89"]  
  2.   
  3. def IPv4Ptn = /\b([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\b/  
  4. hosts.each{ host->  
  5.     match = host =~ IPv4Ptn  
  6.     if (match.find())  
  7.     {  
  8.         printf "'%s' matches IPv4 format!\n", match[0][0]  
  9.         printf "\tPart1=%s\n", match[0][1]  
  10.         printf "\tPart2=%s\n", match[0][2]  
  11.         printf "\tPart3=%s\n", match[0][3]  
  12.         printf "\tPart4=%s\n", match[0][4]  
  13.         println ""  
  14.     }  
  15.     else  
  16.     {  
  17.         printf "'%s' does't match IPv4 format!\n", host  
  18.     }  
  19. }  
如果是 Accurate 版本: 
  1. 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]?)$/  
執行結果: 

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!