【问题标题】:IP REGEX validationIP 正则表达式验证
【发布时间】:2011-11-18 03:09:17
【问题描述】:

我一直在尝试验证输入的字符串(在本例中为sys argv[1])。我需要创建一个脚本,该脚本通过日志文件并将源和目标 ip 的条目与脚本的任何参数输入相匹配。有效输入的种类是

  • 一个IP或部分IP
  • “any”(字符串,表示给定列中的所有 IP 地址)。

到目前为止,我有以下代码。每当我在 bash 中运行脚本以及一个参数(例如任何随机数或单词/字母等)时,我都会出错。请让我知道如何修复它们。非常感谢一种根据 IP 地址 reg ex 和单词 any 验证输入的方法。

#!/usr/bin/python

import sys,re

def ipcheck(ip):
    #raw patterns for "any" and "IP":
    ippattern = '([1-2]?[0-9]?[0-9]\.){1,3}([1-2]?[0-9]?[0-9])?'
    anypattern = any
    #Compiled patterns
    cippattern = re.compile(ippattern)
    canypattern = re.compile(any)
    #creating global variables for call outside function    
    global matchip
    global matchany
    #matching the compiled pattern 
    matchip = cippattern.match(ip)
    matchany = canypattern.match(ip)

new = sys.argv[1]
snew = str(new)
print type(snew)
ipcheck(new)

我也尝试这样做,但它一直给我错误,是否可以通过“OR |”将 2 个参数传递给 if 循环操作员?我该怎么做呢?[/b]

#if (matchip | matchany) :  
    #print "the ip address is valid"
#else:
    #print "Invalid Destination IP"


Error
========================

user@bt:/home# ./ipregex.py a
<type 'str'>
Traceback (most recent call last):
File "./ipregex.py", line 21, in <module>
ipcheck(new)
File "./ipregex.py", line 15, in ipcheck
matchany = re.match(anypattern,ip)
File "/usr/lib/python2.5/re.py", line 137, in match
return _compile(pattern, flags).match(string)
File "/usr/lib/python2.5/re.py", line 237, in _compile
raise TypeError, "first argument must be string or compiled pattern"
TypeError: first argument must be string or compiled pattern

================================================ ===========

编辑

我试图在不编译正则表达式的情况下匹配 IP。所以我修改了脚本来这样做。这导致了错误:

错误

user@bt:/home# ./ipregex.py a
<type 'str'>
Traceback (most recent call last):
File "./ipregex.py", line 21, in <module>
ipcheck(new)
File "./ipregex.py", line 15, in ipcheck
matchany = anypattern.match(ip)
AttributeError: 'builtin_function_or_method' object has no attribute 'match'

================================================ ===========

编辑#2

我能够在更简单的代码版本中重现我的错误。我到底做错了什么??????

#!/usr/bin/python

import sys
import re

def ipcheck(ip):
    anypattern = any
    cpattern = re.compile(anypattern)
    global matchany
    matchany = cpattern.match(ip)
    if matchany:
            print "ip match: %s" % matchany.group()
new = sys.argv[1]
ipcheck(new)

错误

user@bt:/home# ./test.py any
Traceback (most recent call last):
File "./test.py", line 14, in <module>
ipcheck(new)
File "./test.py", line 8, in ipcheck
cpattern = re.compile(anypattern)
File "/usr/lib/python2.5/re.py", line 188, in compile
return _compile(pattern, flags)
File "/usr/lib/python2.5/re.py", line 237, in _compile
raise TypeError, "first argument must be string or compiled pattern"
TypeError: first argument must be string or compiled pattern

【问题讨论】:

  • 当 IPv6 越来越相关和需要时,为什么您只匹配 IPv4?
  • 我应该解析的日志文件只包含ipv4地址。
  • 如果您想匹配所有内容,请将 anypattern = any 更改为 anypattern = "*"。如果不是,变量any 来自哪里?目前,该脚本正在尝试编译内置函数any()docs.python.org/library/functions.html#any
  • 哦,我终于看到了!我不敢相信我将内置函数名称与变量混合在一起。我将 anypattern = any 转换为 anypattern = 'any' (因为我想搜索确切的字符串 - any),它现在可以工作了! :D

标签: python regex validation loops


【解决方案1】:

当您使用re.compile 时,您在编译对象上调用match 函数:ippattern.match(ip)。此外,要从 MatchObject 获取匹配的 IP,请使用 MatchObject.group()。修正了一些你的例子,它现在应该做你需要的:

#!/usr/bin/python

import sys
import re

def ipcheck(ip):
    ippattern_str = '(([1-2]?[\d]{0,2}\.){1,3}([1-2]?[\d]{0,2})|any)'

    ippattern = re.compile(ippattern_str)
    # ippattern is now used to call match, passing only the ip string
    matchip = ippattern.match(ip)
    if matchip:
        print "ip match: %s" % matchip.group()

if len(sys.argv) > 1:
    ipcheck(sys.argv[1])

一些结果:

[ 19:46 jon@hozbox ~/SO/python ]$ ./new.py 100.
ip match: 100.
[ 19:46 jon@hozbox ~/SO/python ]$ ./new.py 100.1.
ip match: 100.1.
[ 19:46 jon@hozbox ~/SO/python ]$ ./new.py 100.1.55.
ip match: 100.1.55.
[ 19:46 jon@hozbox ~/SO/python ]$ ./new.py 100.1.55.255
ip match: 100.1.55.255
[ 19:47 jon@hozbox ~/SO/python ]$ ./new.py any
ip match: any
[ 19:47 jon@hozbox ~/SO/python ]$ ./new.py foo
[ 19:47 jon@hozbox ~/SO/python ]$ 

【讨论】:

  • 我更新了我的代码以包含正则表达式的编译版本。但即使在这样做之后,我仍然在编辑中列出了错误。
  • 您需要将compile调用的返回值用于match。查看我的编辑。
  • 我复制并粘贴了您的脚本,它运行良好,我尝试将其与我的脚本进行比较,但我仍然没有发现任何差异..不知道我做错了什么,也许一些我看不到的隐藏字符是搞砸了?
  • 可能的;它很容易在正则表达式中输入 1 个字符并弄乱整个模式。我已经做了太多次了:)。
【解决方案2】:

这个正则表达式可能更好:

((([1-2]?[0-9]?[0-9]\.){1,3}([1-2]?[0-9]?[0-9])?)|any)

它将匹配任何内容:

127.0.0.1
127.0.0
127.0
127.
192.168.1.1
any

您的正则表达式会遇到上述问题,因为它与 0 不匹配。

编辑:

我错过了匹配any的部分。

这个正则表达式会匹配一些无效的地址,但是如果你只是搜索日志文件应该没问题。如果您确实需要准确,不妨查看this link

【讨论】:

  • 看来他也想匹配“any”这个词。您应该使用“|any”更新正则表达式。
  • 欣赏用于 IP 验证的新的和改进的正则表达式。 @chown 如果您查看脚本中间的注释行,您会看到我试图实现“OR” |运营商没有任何成功,像这样:#if (matchip | matchany) : #print "IP 地址有效" #else: #print "Invalid Destination IP" 你有什么建议吗?
  • @A 检查我的编辑,正则表达式模式中内置了 OR(表示“任何”),请查看正则表达式模式的最后一部分。
  • @chown 非常感谢! :D 但我认为它不可能在 if 循环中包含 2 个条件。我知道它可能使用数字,即如果 2 > x
  • 有可能,您是否希望“any”和 ip 是 2 个不同的正则表达式模式彼此分开?生病更新以显示一个例子。
猜你喜欢
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
  • 2017-04-12
  • 1970-01-01
  • 2016-08-24
  • 2021-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多