程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了专用 IP 地址的正则表达式大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决专用 IP 地址的正则表达式?

开发过程中遇到专用 IP 地址的正则表达式的问题如何解决?下面主要结合日常开发的经验,给出你关于专用 IP 地址的正则表达式的解决方法建议,希望对你解决专用 IP 地址的正则表达式有所启发或帮助;

我是编码和 python 的新手,我想知道如何创建一个正则表达式来匹配所有以 192.168.1.xxx 开头的 IP 地址,我一直在网上查找,但还没有找到匹配项。这是我试图从中匹配它们的一些示例数据

?- flip([a,b,c],[c,a]).
true

?- flip([c],[c]).
true

?- flip([a,[c]).
false

?- flip([],[]).
true

解决方法

给你。另外,结帐https://regexr.com/

^192\.168\.1\.[0-9]{1,3}$

,

如果你真的只想匹配'192.168.1.xxx',那么你可以使用这个正则表达式在python中具体使用它:“192\.168\.1\.[0-9]{1,3 }”。

我个人建议使用 regexr 来更熟悉正则表达式。您可以输入数据,然后在左侧查看备忘单以帮助您学习。

,

我认为这里最好使用 regex 的组合从您的数据中逐行获取任何有效的 IP 地址。然后使用 ipaddress 检查地址是否位于您要查找的网络内。

这将在您需要检查不同网络的情况下提供更大的灵活性,而不是每次都重写 regex,您可以创建一个 ip_network 对象。我们还可以创建多个网络,并检查所有网络是否存在。

import ipaddress
import re

data =  '''/index.html http/1.1" 404 208 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET / http/1.1" 403 4897 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET /noindex/css/fonts/Light/OpenSans-Light.woff http/1.1" 404 241 "http://optiplex360/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET /noindex/css/fonts/Bold/OpenSans-Bold.woff http/1.1" 404 239 "http://optiplex360/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf http/1.1" 404 240 "http://optiplex360/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET /noindex/css/fonts/Bold/OpenSans-Bold.ttf http/1.1" 404 238 "http://optiplex360/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:53 -0400] "GET /first http/1.1" 404 203 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.1 - - [30/Sep/2016:16:19:00 -0400] "GET /HNAP1/ http/1.1" 404 204 "-" "-"
192.168.1.1 - - [30/Sep/2016:16:19:00 -0400] "GET / http/1.1" 403 4897 "-" "-"
192.168.1.1 - - [30/Sep/2016:16:19:00 -0400] "POST /JNAP/ http/1.1" 404 203 "-" "-"
192.168.1.1 - - [30/Sep/2016:16:19:00 -0400] "POST /JNAP/ http/1.1" 404 203 "-" "-"'''

network = ipaddress.ip_network('192.168.1.0/24')

# Pattern that matches any valid ipv4 address
pattern = r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'

for row in data.split():
    if (ip := re.search(pattern,row)):
        if ipaddress.IPv4Address(ip.group()) in network:
            print(f'{ip.group()} exists in {network}')

输出

192.168.1.142 exists in 192.168.1.0/24
192.168.1.142 exists in 192.168.1.0/24
192.168.1.142 exists in 192.168.1.0/24
192.168.1.142 exists in 192.168.1.0/24
192.168.1.142 exists in 192.168.1.0/24
192.168.1.142 exists in 192.168.1.0/24
192.168.1.1 exists in 192.168.1.0/24
192.168.1.1 exists in 192.168.1.0/24
192.168.1.1 exists in 192.168.1.0/24
192.168.1.1 exists in 192.168.1.0/24

大佬总结

以上是大佬教程为你收集整理的专用 IP 地址的正则表达式全部内容,希望文章能够帮你解决专用 IP 地址的正则表达式所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:专用