博客
关于我
正则表达式匹配IP地址
阅读量:551 次
发布时间:2019-03-09

本文共 696 字,大约阅读时间需要 2 分钟。

IP地址由四个字节构成,每个字节的范围在0到255之间。为了有效匹配IP地址的各个部分,可以将它们分成两部分进行处理:前三部分和后三部分。

首先,分析每个IP段的范围

  • 0到255可以进一步分为两部分:

    • 0到199:可以表示为 [0-1]?\\d{1,2}

      • [0-1]? 表示0或1作为前导数字,0次或1次。
      • \\d{1,2} 表示1或2个数字,范围在0到9之间。
    • 200到255:可以表示为 2((5[0-5])|[0-4]\\d)

      • 2 表示以2开头。
      • (5[0-5]) 表示50到55之间的数字。
      • [0-4]\\d 表示00到49之间的数字。
  • 因此,完整的正则表达式可以分为以下几个部分:

    • (2((5[0-5])|[0-4]\\d))[0-1]?\\d{1,2}
  • 第二部分,点号后面的IP段

  • 点号后接的0到255部分的正则表达式为:.后跟上述两种表达式
  • 第三部分,点号后面的IP段的重复匹配

    为了确保点号后面的IP段出现三次,正则表达式可以写为:\\.后面跟着上述两种表达式\\)重复三次`.

    综合起来

  • IP地址的整个结构可以表示为:^ (整体表达式) $ where:((2((5[0-5])|[0-4]\d))|[0-1]?\d{1,2})\.重复三次.

  • 例如:

    • `^((2((5[0-5])|[0-4]\d))|[0-1]?\d{1,2}){3}.((2((5[0-5])|[0-4]\d))|[0-1]?\d{1,2})){3}$这将确保整个IP地址按照指定格式出现三次.
  • 这样的结构确保了只有以.^ triplet结构出现的IP地址才被匹配,符合标准的网络格式要求。

    转载地址:http://wqksz.baihongyu.com/

    你可能感兴趣的文章
    Nmap扫描教程之Nmap基础知识
    查看>>
    Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
    查看>>
    NMAP网络扫描工具的安装与使用
    查看>>
    NMF(非负矩阵分解)
    查看>>
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>