正则校验IPv4 IPv6
时间: 2024-01-11 10:21:07 浏览: 173
以下是正则校验IPv4和IPv6的示例:
1. 正则校验IPv4:
```javascript
const IPV4Single = (ip) => {
const IPV4 = /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/;
return IPV4.test(ip); // 输出 校验通过:true 校验出错:false
};
```
2. 正则校验IPv6:
```javascript
const IPV6Single = (ip) => {
const IPV6 = /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/;
return IPV6.test(ip); // 输出 校验通过:true 校验出错:false
};
```
相关问题
ipv4 校验 正则表达式 ? ipv6正则表达式校验? ip 段 大小校验 正则表达式?
IPv4校验正则表达式:
```
^(?:(?:^|\.)(?:2(?:5[0-5]|[0-4]\d)|1?\d{1,2})){4}$
```
IPv6校验正则表达式:
```
^(?:(?:(?:(?:(?:(?:[0-9A-Fa-f]{1,4}:){6}|(?=(?:.*[0-9A-Fa-f]){2,})(?=(?:.*[0-9A-Fa-f]:){0,5}:)(([0-9A-Fa-f]{1,4}:){1,5}|:)((:[0-9A-Fa-f]{1,4}){1,5}:|:))((25[0-5]|2[0-4][0-9]|[0-1][0-9]{0,2})\.){3}(25[0-5]|2[0-4][0-9]|[0-1][0-9]{0,2}))|(?:(?:(?:[0-9A-Fa-f]{1,4}:){5}(?:[0-9A-Fa-f]{1,4}))|(?=(?:.*[0-9A-Fa-f]){2,})(?=(?:.*[0-9A-Fa-f]:){0,4}:)(([0-9A-Fa-f]{1,4}:){1,4}|:)((:[0-9A-Fa-f]{1,4}){1,4}|:)(:[0-9A-Fa-f]{1,4}){1,4}|(?:(?:(?:[0-9A-Fa-f]{1,4}:){4}(?:[0-9A-Fa-f]{1,4})){2})|(?=(?:.*[0-9A-Fa-f]){2,})(?=(?:.*[0-9A-Fa-f]:){0,3}:)(([0-9A-Fa-f]{1,4}:){1,3}|:)((:[0-9A-Fa-f]{1,4}){1,3}|:)(:[0-9A-Fa-f]{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]{1,4})){1}(?::[0-9A-Fa-f]{1,4}){1,5})|(?:(?:(?:[0-9A-Fa-f]{1,4}:){1,3}(?:[0-9A-Fa-f]{1,4})){1}(?::[0-9A-Fa-f]{1,4}){1,4})|(?:(?:(?:[0-9A-Fa-f]{1,4}:){1,4}(?:[0-9A-Fa-f]{1,4})){1}(?::[0-9A-Fa-f]{1,4}){1,3})|(?:(?:(?:[0-9A-Fa-f]{1,4}:){1,5}(?:[0-9A-Fa-f]{1,4})){1}(?::[0-9A-Fa-f]{1,4}){1,2})|(?:(?:(?:[0-9A-Fa-f]{1,4}:){1,6}(?:[0-9A-Fa-f]{1,4})){1}(?::[0-9A-Fa-f]{1,4}){1})|(?:(?:(?:[0-9A-Fa-f]{1,4}:){1,7}(?:[0-9A-Fa-f]{1,4})){1}(?::[0-9A-Fa-f]{1,4}))|(?:::(?:[0-9A-Fa-f]{1,4}:){1,6}|(?:(?:(?:[0-9A-Fa-f]{1,4}:){1,6}(?:[0-9A-Fa-f]{1,4})){1}(?::[0-9A-Fa-f]{1,4}){0,1})|(?:(?:(?:[0-9A-Fa-f]{1,4}:){1,5}(?:[0-9A-Fa-f]{1,4})){1}(?::[0-9A-Fa-f]{1,4}){0,2})|(?:(?:(?:[0-9A-Fa-f]{1,4}:){1,4}(?:[0-9A-Fa-f]{1,4})){1}(?::[0-9A-Fa-f]{1,4}){0,3})|(?:(?:(?:[0-9A-Fa-f]{1,4}:){1,3}(?:[0-9A-Fa-f]{1,4})){1}(?::[0-9A-Fa-f]{1,4}){0,4})|(?:(?:(?:[0-9A-Fa-f]{1,4}:){1,2}(?:[0-9A-Fa-f]{1,4})){1}(?::[0-9A-Fa-f]{1,4}){0,5})|(?:(?:[0-9A-Fa-f]{1,4}:){1}(?::[0-9A-Fa-f]{1,4}){0,6}))))(/\d{1,2})?$
```
这三个正则表达式可以用于校验IP地址和IP段大小的格式。
java语言实现ip的正则校验
A:下面是Java语言实现IP地址正则校验的示例代码:
```
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class IPValidator {
private static final String IP_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])$";
private static final Pattern pattern = Pattern.compile(IP_PATTERN);
public static boolean validateIP(String ip) {
if (ip == null || ip.isEmpty()) {
return false;
}
Matcher matcher = pattern.matcher(ip);
return matcher.matches();
}
public static void main(String[] args) {
String ip1 = "192.168.0.1";
String ip2 = "10.0.0.256";
String ip3 = "abc.def.ghi.jkl";
System.out.println(ip1 + " is valid? " + validateIP(ip1));
System.out.println(ip2 + " is valid? " + validateIP(ip2));
System.out.println(ip3 + " is valid? " + validateIP(ip3));
}
}
```
在代码中,我们使用了正则表达式 `^([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])$` 对IP地址进行了校验。该正则表达式可以匹配符合IPv4标准的IP地址。然后,我们使用`Pattern.matcher()`方法将输入的IP地址与正则表达式进行匹配并返回验证结果。最后,在`main`方法中我们对三个不同的IP地址进行验证。
输出结果如下所示:
```
192.168.0.1 is valid? true
10.0.0.256 is valid? false
abc.def.ghi.jkl is valid? false
```
示例代码中的正则表达式可用于校验符合IPv4标准的IP地址,但不包括IPv6地址。如果您需要校验IPv6地址,需要编写相应的正则表达式或使用IPv6库进行验证。
阅读全文