简体   繁体   English

如何检查字符串是否为 Groovy 中的 IP?

[英]How can I check if a String is an IP in Groovy?

From a given String:从给定的字符串:

String someIp = // some String

How can I check, if someIp is a valid Ip format?我如何检查 someIp 是否是有效的 Ip 格式?

You can use InetAddressValidator class to check and validate weather a string is a valid ip or not. 您可以使用InetAddressValidator类来检查和验证字符串是否是有效的IP。

import org.codehaus.groovy.grails.validation.routines.InetAddressValidator

...
String someIp = // some String
if(InetAddressValidator.getInstance().isValidInet4Address(someIp)){
    println "Valid Ip"
} else {
    println "Invalid Ip"
}
...

Try this..,. 试试这个..,。

Regexes will do. 正则表达式会做。 There are simple ones and more complex. 有简单的,更复杂的。 A simple one is this regex: 一个简单的就是这个正则表达式:

\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}

Use it like this: 像这样使用它:

boolean isIP = someIP.maches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");

But this will match 999.999.999.999 as well, which is not a valid IP address. 但这也将匹配999.999.999.999,这不是有效的IP地址。 There is a huge regex available on regular-expressions.info : regular-expressions.info上有一个巨大的正则表达式:

(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]?)

This one will take care of the job correctly. 这个将正确地处理工作。 If you use this one, don't forget to escape every \\ with another \\ . 如果你使用这个,不要忘记逃避每个\\与另一个\\


If you are not a fan of huge regexes, you can use this code: 如果您不是大型正则表达式的粉丝,可以使用以下代码:

public static boolean isIP(String str)
{
    try
    {
         String[] parts = str.split("\\.");
         if (parts.length != 4) return false;
         for (int i = 0; i < 4; ++i)
         {
             int p = Integer.parseInt(parts[i]);
             if (p > 255 || p < 0) return false;
         }
         return true;
    } catch (Exception e)
    {
        return false;
    }
}

An oriented object way: 面向对象的方式:

String myIp ="192.168.43.32"
myIp.isIp();

Known that , you must add this to BootStrap.groovy : 已知,您必须将此添加到BootStrap.groovy

String.metaClass.isIp={
   if(org.codehaus.groovy.grails.validation.routines.InetAddressValidator.getInstance().isValidInet4Address(delegate)){
    return true;
   } else {
    return false;
    } 


}

Use a validator like Apache's commons-validator :使用像 Apache 的commons-validator这样的验证器:

@Grab('commons-validator:commons-validator:1.7')
import org.apache.commons.validator.routines.InetAddressValidator

boolean isIpV4(String ip) {
    InetAddressValidator.instance.isValidInet4Address(ip)
}

isIpV4("8.8.8.8")

Or regex:或正则表达式:

import java.util.regex.Pattern

class RegexValidationConstants {
    private final static String IPV4_OCT = /(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/
    final static Pattern IPV4 = ~/^${IPV4_OCT}(\.${IPV4_OCT}){3}$/.toString()
}

boolean isIpV4(String ip) {
    ip ==~ RegexValidationConstants.IPV4
}

isIpV4("8.8.8.8")

If you are using Grails, see my answer in this other thread .如果您使用的是 Grails,请参阅我在另一个线程中的回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM