2013-04-08 28 views
10

在應用程序中,我得到包含IP地址的字符串,但這些字符串沒有精確的格式。 我們只知道這些字符串可能包含IP地址。使用正則表達式從字符串中提取IP地址

這裏的字符串可以是什麼樣子的例子:

  • 「XPSPort」
  • 「IP_10.29.167.187」
  • 「10.29.166.193」

我會就像獲取一個提取該字符串的ip地址的Java代碼(如果有的話),或者如果該字符串不包含ip地址,則返回「」。

我想這個代碼,但它不工作:

String IPADDRESS_PATTERN = 
     "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + 
     "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + 
     "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + 
     "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"; 

Pattern pattern = Pattern.compile(IPADDRESS_PATTERN); 
Matcher matcher = pattern.matcher(ipString); 
     if (matcher.find()) { 
      return matcher.group(); 
     } 
     else{ 
      return "0.0.0.0"; 
     } 

我敢肯定,使用正則表達式是實現這一目標的最佳途徑,但我不是非常好,這些所謂有人可以幫助我找到好的RegExp?

在此先感謝。

+0

可能重複(HTTP:/ /stackoverflow.com/questions/8439633/regex-ip-address-from-string) – Richard 2013-04-08 09:01:11

+1

謝謝理查德,這不完全是重複的,因爲在我的c因爲知識產權並不總是一個完整的詞,但它幫助我找到答案。 – Padrus 2013-04-08 09:17:10

回答

27

理查德的鏈接幫助我找到答案。 這裏的工作代碼:

String IPADDRESS_PATTERN = 
     "(?:(?: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]?)"; 

Pattern pattern = Pattern.compile(IPADDRESS_PATTERN); 
Matcher matcher = pattern.matcher(ipString); 
if (matcher.find()) { 
    return matcher.group(); 
} else{ 
    return "0.0.0.0"; 
} 
+0

當給一個字符串http://256.225.255.255:8690/AppPortal/返回ip地址爲56.225.255.255。 – Arundev 2014-06-16 11:16:23

+5

這是因爲256.255.255.255不是一個有效的IP地址,最大的數字是255. – Padrus 2014-06-17 14:53:27

+1

這個工作的原因是因爲^和$不存在。如果沒有這些字符,你的原始正則表達式也會起作用。這些字符表示字符串的開始和結束,因此包含這兩個字符串的正則表達式不會匹配字符串中找到的地址。 – 2016-10-19 01:58:52

11

IPV4_PATTERN = "^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$"

9

檢查該解決方案可以同時驗證IPV4 & IPv6地址

/** 
* This class provides a variety of basic utility methods that are not 
* dependent on any other classes within the org.jamwiki package structure. 
*/ 
public class Utilities { 
    private static Pattern VALID_IPV4_PATTERN = null; 
    private static Pattern VALID_IPV6_PATTERN = null; 
    private static final String ipv4Pattern = "(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])"; 
    private static final String ipv6Pattern = "([0-9a-f]{1,4}:){7}([0-9a-f]){1,4}"; 

    static { 
    try { 
     VALID_IPV4_PATTERN = Pattern.compile(ipv4Pattern, Pattern.CASE_INSENSITIVE); 
     VALID_IPV6_PATTERN = Pattern.compile(ipv6Pattern, Pattern.CASE_INSENSITIVE); 
    } catch (PatternSyntaxException e) { 
     //logger.severe("Unable to compile pattern", e); 
    } 
    } 

    /** 
    * Determine if the given string is a valid IPv4 or IPv6 address. This method 
    * uses pattern matching to see if the given string could be a valid IP address. 
    * 
    * @param ipAddress A string that is to be examined to verify whether or not 
    * it could be a valid IP address. 
    * @return <code>true</code> if the string is a value that is a valid IP address, 
    * <code>false</code> otherwise. 
    */ 
    public static boolean isIpAddress(String ipAddress) { 

    Matcher m1 = Utilities.VALID_IPV4_PATTERN.matcher(ipAddress); 
    if (m1.matches()) { 
     return true; 
    } 
    Matcher m2 = Utilities.VALID_IPV6_PATTERN.matcher(ipAddress); 
    return m2.matches(); 
    } 
} 

源:Determine if the given string is a valid IPv4 or IPv6 address.

0

IP地址正則表達式模式:

^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\. 
([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$ 

了用實施例參考點擊here

[從字符串的正則表達式的IP地址]的