用正则表达式检查是ip否为内网地址
- PHP
php
function isIntranet($ip)
{
/* IPV4内网地址
A 类10.0.0.0~10.255.255.255
B 类172.16.0.0~172.31.255.255
C 类192.168.0.0~192.168.255.255
*/
// 检查是否为 IPv4 内网地址
if (preg_match('/^10\./', $ip)) return true;
if (preg_match('/^172\.(1[6-9]|2[0-9]|3[0-1])\./', $ip)) return true;
if (preg_match('/^192\.168\./', $ip)) return true;
/* IPV6内网地址
ULA地址: fc00::/7 (fc00:: - fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff)
链路本地地址: fe80::/10 (fe80:: - febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff)
*/
// 检查是否为 IPv6 内网地址
if (preg_match('/^f[c-d][0-9a-f]{2}:/i', $ip)) return true; // 匹配 fc00::/7 (包括 fc00:: 和 fdff:: 范围)
if (preg_match('/^fe[89ab][0-9a-f]:/i', $ip)) return true; // 匹配 fe80::/10 (包括 fe80:: 到 febf:: 范围)
return false;
}
- Java
java
import java.util.regex.Pattern;
public class IPUtils {
public static boolean isIntranet(String ip) {
// IPV4 内网地址正则表达式
String ipv4Pattern10 = "^10\\.";
String ipv4Pattern172 = "^172\\.(1[6-9]|2[0-9]|3[0-1])\\.";
String ipv4Pattern192 = "^192\\.168\\.";
// IPV6 内网地址正则表达式
String ipv6PatternFC = "^f[c-d][0-9a-f]{2}:";
String ipv6PatternFE = "^fe[89ab][0-9a-f]:";
// 匹配 IPv4 内网地址
if (Pattern.matches(ipv4Pattern10, ip)) return true;
if (Pattern.matches(ipv4Pattern172, ip)) return true;
if (Pattern.matches(ipv4Pattern192, ip)) return true;
// 匹配 IPv6 内网地址
if (Pattern.matches(ipv6PatternFC, ip)) return true;
if (Pattern.matches(ipv6PatternFE, ip)) return true;
return false;
}
public static void main(String[] args) {
System.out.println(isIntranet("10.0.0.1")); // true
System.out.println(isIntranet("fc00::1")); // true
System.out.println(isIntranet("172.16.0.1")); // true
System.out.println(isIntranet("192.168.0.1")); // true
System.out.println(isIntranet("fe80::1")); // true
System.out.println(isIntranet("8.8.8.8")); // false
}
}
- Golang
go
package main
import (
"fmt"
"regexp"
)
func isIntranet(ip string) bool {
// 定义 IPv4 内网地址的正则表达式
ipv4Pattern10 := `^10\.`
ipv4Pattern172 := `^172\.(1[6-9]|2[0-9]|3[0-1])\.`
ipv4Pattern192 := `^192\.168\.`
// 定义 IPv6 内网地址的正则表达式
ipv6PatternFC := `^f[c-d][0-9a-f]{2}:`
ipv6PatternFE := `^fe[89ab][0-9a-f]:`
// 匹配 IPv4 内网地址
if matched, _ := regexp.MatchString(ipv4Pattern10, ip); matched {
return true
}
if matched, _ := regexp.MatchString(ipv4Pattern172, ip); matched {
return true
}
if matched, _ := regexp.MatchString(ipv4Pattern192, ip); matched {
return true
}
// 匹配 IPv6 内网地址
if matched, _ := regexp.MatchString(ipv6PatternFC, ip); matched {
return true
}
if matched, _ := regexp.MatchString(ipv6PatternFE, ip); matched {
return true
}
return false
}
func main() {
fmt.Println(isIntranet("10.0.0.1")) // true
fmt.Println(isIntranet("fc00::1")) // true
fmt.Println(isIntranet("172.16.0.1")) // true
fmt.Println(isIntranet("192.168.0.1")) // true
fmt.Println(isIntranet("fe80::1")) // true
fmt.Println(isIntranet("8.8.8.8")) // false
}
- C
c
#include <stdio.h>
#include <stdbool.h>
#include <regex.h>
bool isIntranet(const char *ip) {
// 定义正则表达式的模式和个数
const char *patterns[] = {
"^10\\.", // IPv4 10.x.x.x
"^172\\.(1[6-9]|2[0-9]|3[0-1])\\.", // IPv4 172.16.x.x - 172.31.x.x
"^192\\.168\\.", // IPv4 192.168.x.x
"^f[c-d][0-9a-f]{2}:", // IPv6 fc00::/7
"^fe[89ab][0-9a-f]:" // IPv6 fe80::/10
};
const int pattern_count = sizeof(patterns) / sizeof(patterns[0]);
regex_t regex[pattern_count];
bool result = false;
// 编译所有正则表达式
for (int i = 0; i < pattern_count; i++) {
if (regcomp(®ex[i], patterns[i], REG_EXTENDED | REG_ICASE) != 0) {
// 编译失败时释放已分配的正则表达式资源
for (int j = 0; j < i; j++) regfree(®ex[j]);
return false;
}
}
// 匹配所有模式
for (int i = 0; i < pattern_count; i++) {
if (regexec(®ex[i], ip, 0, NULL, 0) == 0) {
result = true;
break;
}
}
// 释放正则表达式资源
for (int i = 0; i < pattern_count; i++) {
regfree(®ex[i]);
}
return result;
}
int main() {
printf("%d\n", isIntranet("10.0.0.1")); // 1 (true)
printf("%d\n", isIntranet("fc00::1")); // 1 (true)
printf("%d\n", isIntranet("172.16.0.1")); // 1 (true)
printf("%d\n", isIntranet("192.168.0.1")); // 1 (true)
printf("%d\n", isIntranet("fe80::1")); // 1 (true)
printf("%d\n", isIntranet("8.8.8.8")); // 0 (false)
return 0;
}
- C++
cpp
#include <iostream>
#include <regex>
#include <string>
bool isIntranet(const std::string& ip) {
// 定义 IPv4 内网地址的正则表达式
std::regex ipv4Pattern10("^10\\.");
std::regex ipv4Pattern172("^172\\.(1[6-9]|2[0-9]|3[0-1])\\.");
std::regex ipv4Pattern192("^192\\.168\\.");
// 定义 IPv6 内网地址的正则表达式
std::regex ipv6PatternFC("^f[c-d][0-9a-f]{2}:", std::regex::icase);
std::regex ipv6PatternFE("^fe[89ab][0-9a-f]:", std::regex::icase);
// 匹配 IPv4 内网地址
if (std::regex_search(ip, ipv4Pattern10)) return true;
if (std::regex_search(ip, ipv4Pattern172)) return true;
if (std::regex_search(ip, ipv4Pattern192)) return true;
// 匹配 IPv6 内网地址
if (std::regex_search(ip, ipv6PatternFC)) return true;
if (std::regex_search(ip, ipv6PatternFE)) return true;
return false;
}
int main() {
std::cout << isIntranet("10.0.0.1") << std::endl; // true
std::cout << isIntranet("fc00::1") << std::endl; // true
std::cout << isIntranet("172.16.0.1") << std::endl; // true
std::cout << isIntranet("192.168.0.1") << std::endl; // true
std::cout << isIntranet("fe80::1") << std::endl; // true
std::cout << isIntranet("8.8.8.8") << std::endl; // false
return 0;
}