用正则表达式检查是IP否为内网地址

用正则表达式检查是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(&regex[i], patterns[i], REG_EXTENDED | REG_ICASE) != 0) {
            // 编译失败时释放已分配的正则表达式资源
            for (int j = 0; j < i; j++) regfree(&regex[j]);
            return false;
        }
    }

    // 匹配所有模式
    for (int i = 0; i < pattern_count; i++) {
        if (regexec(&regex[i], ip, 0, NULL, 0) == 0) {
            result = true;
            break;
        }
    }

    // 释放正则表达式资源
    for (int i = 0; i < pattern_count; i++) {
        regfree(&regex[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;
}
相关推荐
RainbowSea几秒前
14. MySQL 锁的详细说明
java·sql·mysql
是2的10次方啊1 分钟前
🔄 Bean属性转换框架深度对比:从BeanUtils到MapStruct的演进之路
java·后端
小庞在加油15 分钟前
《dlib库中的聚类》算法详解:从原理到实践
c++·算法·机器学习·数据挖掘·聚类
RainbowSea17 分钟前
12 MySQL 数据库其它调优策略
java·sql·mysql
ComputerInBook18 分钟前
C++ 标准模板库算法之 transform 用法
开发语言·c++·算法·transform算法
杰哥技术分享26 分钟前
PHP Yii2 安装SQL Server扩展-MAC M4 Pro芯片
开发语言·php
大只鹅36 分钟前
WebSocket类明明注入了Bean,为什么报错为null
java·websocket
ChinaRainbowSea44 分钟前
9-2 MySQL 分析查询语句:EXPLAIN(详细说明)
java·数据库·后端·sql·mysql
时序数据说1 小时前
Java类加载机制及关于时序数据库IoTDB排查
java·大数据·数据库·物联网·时序数据库·iotdb
wowocpp1 小时前
rabbitmq 与 Erlang 的版本对照表 win10 安装方法
java·rabbitmq·erlang