ip地址正则表达式java
时间: 2024-01-28 16:13:58 浏览: 101
IP地址正则表达式
以下是一个使用正则表达式验证IP地址是否合法的Java程序示例:
```java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class IPAddressValidator {
private static final String IP_ADDRESS_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])$";
public static boolean validateIPAddress(String ipAddress) {
Pattern pattern = Pattern.compile(IP_ADDRESS_PATTERN);
Matcher matcher = pattern.matcher(ipAddress);
return matcher.matches();
}
public static void main(String[] args) {
String ipAddress = "192.168.0.1";
boolean isValid = validateIPAddress(ipAddress);
System.out.println("Is IP address valid? " + isValid); // 输出:Is IP address valid? true
}
}
```
阅读全文