- 10.0.0.0 - 10.255.255.255 (10/8 prefix)
- 172.16.0.0 - 172.31.255.255 (172.16/12 prefix)
- 192.168.0.0 - 192.168.255.255 (192.168/16 prefix)
參考的程式碼如下:
public void getPublicIP (String[] ipArray) {
  // Regular expression for validating the IP address.
  Pattern ipv4 = Pattern.compile(
    "(^127\\.0\\.0\\.1)|(^10\\.)|(^172\\.1[6-9]\\.)|(^172\\.2[0-9]\\.)|(^172\\.3[0-1]\\.)|(^192\\.168\\.)|localhost");
  Matcher regexMatcher = null;
  for(int i=0 ; i<ipArray.length() ; i++) {
    String currentIP = ipArray[i];
    if(currentIP .contains(".")) {
      // IPv4
      regexMatcher = ipv4.matcher(currentIP );
      if(!regexMatcher.find()) {
        System.out.println("Public IP: " + currentIP );
      }
    }
  }
}
參考資料:
1、RFC1918
2、Private IP Address Identifier in Regular Expression
另外推薦一個小軟體 RegexBuddy,同事 Ken 神推薦的!
可以測試 Regular Expression,然後也可以把 RE 輸出成各種語言的語法!
雖然官網上沒有 eveluation 的版本,但似乎以前曾經有過?
我在搜尋時有找到 CNET 有提供 v3.5.5 Demo 版本的下載~。
沒有留言:
張貼留言