用正则表达式检查是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;
}
相关推荐
魔众12 分钟前
ModStartBlog v11.3.0 安全加固、体验优化,自动化测试更强大
开源·php·laravel
残诗14 分钟前
正则表达式分类掌握与应用
正则表达式
郑州光合科技余经理15 分钟前
本地生活平台搭建:跨业态用户标识怎么贯通
java·开发语言·前端·后端·uni-app·php·ai编程
木头科技22 分钟前
【AI 工程化第八篇】Spring AI 多模型路由实战:DeepSeek、通义、Kimi、OpenAI 如何统一接入和自动切换
java·人工智能·spring
HZY1618yzh24 分钟前
下一代编程语言出炉
c++·算法
深盾科技_Virbox24 分钟前
云端与本地并行交付,软件许可管理如何实现统一运营?
java·大数据·开发语言·安全
thefool11226625 分钟前
多线程 3
java
CoderYanger31 分钟前
A.每日一题:856. 括号的分数
java·程序人生·算法·leetcode·面试·职场和发展·学习方法
wyhwust41 分钟前
No1-绪论
c++
无名猿1 小时前
C++ 对象内存模型:vtable 布局、多继承 vptr、RTTI 与 dynamic_cast 走查
c++·内存管理·调试技巧·现代c++