2013-04-08 16 views
10

In un'applicazione, ottengo stringhe contenenti indirizzi IP ma queste stringhe non hanno un formato preciso. Tutto ciò che sappiamo è che queste stringhe possono contenere un indirizzo IP.Estrarre gli indirizzi IP dalle stringhe usando regex

Ecco un esempio di quello che la stringa può apparire come:

  • "XPSPort"
  • "IP_10.29.167.187"
  • "10.29.166.193"

lo farei come ottenere un codice Java che estrae l'indirizzo IP della stringa se ce n'è uno o che restituisce "" se la stringa non contiene un indirizzo IP.

Ho provato questo codice, ma non funziona:

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"; 
     } 

Sono abbastanza sicuro che utilizza RegExp è il modo migliore per raggiungere questo obiettivo, ma io non sono molto bravo con questi in modo qualcuno può aiutarmi trova il buon RegExp?

Grazie in anticipo.

+0

possibile duplicato di [indirizzo IP regex da stringa] (http://stackoverflow.com/questions/8439633/regex-ip-address-from-string) – Richard

+1

Grazie Richard, questo non è esattamente un duplicato perché nella mia c come l'IP non è sempre una parola completa, ma mi ha aiutato a trovare la risposta. – Padrus

risposta

27

collegamento di Richard mi ha aiutato a trovare la risposta. Ecco il codice di lavoro:

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

Quando si fornisce una stringa http://256.225.255.255:8690/AppPortal/ restituendo l'indirizzo ip come 56.225.255.255. – Arundev

+5

perché 256.255.255.255 non è un indirizzo IP valido, il numero massimo è 255. – Padrus

+1

La ragione per cui questo funziona è perché^e $ non ci sono. Il tuo regexp originale avrebbe funzionato anche senza quei caratteri. Questi caratteri indicano l'inizio e la fine della stringa, quindi una regex che contiene quei due non corrisponderà a un indirizzo che si trova all'interno della stringa. –

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

controllare questa soluzione per convalidare sia IPV4 & indirizzi 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(); 
    } 
} 

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

0

Indirizzo IP Regular Expression 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])$ 

Per avere un riferimento con esempi clic here

Problemi correlati