regex 专用IP地址的正则表达式

jjhzyzn0  于 2023-06-25  发布在  其他
关注(0)|答案(3)|浏览(140)

我想知道如何创建一个正则表达式,将匹配所有IP地址,以192.168.1.xxx开始,我一直在网上寻找,还没有能够找到匹配。这里有一些样本数据,我试图匹配它们。

/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 "-" "-"
rggaifut

rggaifut1#

给你此外, checkout https://regexr.com/
^192\.168\.1\.[0-9]{1,3}$

2skhul33

2skhul332#

如果你真的只想匹配'192.168.1.xxx',那么你可以使用这个正则表达式在python中使用:“192.168.1.[0-9]{1,3}”。
我个人推荐使用regexr来更熟悉正则表达式。你可以输入你的数据,在左边你可以看一个备忘录来帮助你学习。

nukf8bse

nukf8bse3#

我认为这里最好使用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

相关问题