打印可选正则表达式

2024-04-27 08:51:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图打印出存储在字典中的值。这些值是从正则表达式创建的。你知道吗

目前,我有一些可选的领域,但我不知道如果我这样做是正确的

(field A(field B)? field C (field D)?)?

我读了一个快速参考,它说?表示0或1次出现。你知道吗

当我试图搜索reputationcontent-type之类的字段时,我得到了None,因为它们在正则表达式中是可选的。我可能有错误的正则表达式,但我想知道为什么每当我搜索可选字段(...)?时,它就会输出None

我的代码:

import re

httpproxy515139 = re.compile(r'....url\=\"(?P<url>(.*))\"(\s+exceptions\=\"(?P<exceptions>([^\"]*))\"\s+error\=\"(?P<error>([^\"]*))\"\s+(reputation\=\"(?P<reputation_opt>([^\"]*))\"\s+)?category\=\"(?P<category>([^\"]*))\"\s+reputation\=\"(?P<reputation>([^\"]*))\"\s+categoryname\=\"(?P<categoryname>([^\"]*))\"\s+(content-type\=\"(?P<content_type>([^\"]*))\")?)?')

f  = open("sophos-httpproxy.out", "r")
fw = open("sophosfilter.log", "w+")

HttpProxyCount = 0
otherCount = 0

for line in f.readlines():
    HttpProxy = re.search(httpproxy515139, line)
    HttpProxy.groupdict()

    print "AV Field: "
    print "Date/Time: " + str(HttpProxy.groupdict()['categoryname'])

下面是完整的正则表达式:

(?P<datetime>\w\w\w\s+\d+\s+\d\d:\d\d:\d\d)\s+(?P<IP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*httpproxy\[(?P<HTTPcode>(.*))\]:\s+id\=\"(?P<id>([^\"]*))\"\s+severity\=\"(?P<Severity>([^\"]*))\"\s+sys\=\"(?P<sys>([^\"]*))\"\s+sub\=\"(?P<sub>([^\"]*))\"\s+name\=\"(?P<name>([^\"]*))\"\s+action\=\"(?P<action>([^\"]*))\"\s+method\=\"(?P<method>([^\"]*))\"\s+srcip\=\"(?P<srcip>([^\"]*))\"\s+dstip\=\"(?P<dstip>([^\"]*))\"\s+user\=\"(?P<user>[^\"]*)\"\s+statuscode\=\"(?P<statuscode>([^\"]*))\"\s+cached\=\"(?P<cached>([^\"]*))\"\s+profile\=\"(?P<profile>([^\"]*))\"\s+filteraction\=\"(?P<filteraction>([^\"]*))\"\s+size\=\"(?P<size>([^\"]*))\"\s+request\=\"(?P<request>([^\"]*))\"\s+url\=\"(?P<url>(.*))\"(\s+exceptions\=\"(?P<exceptions>([^\"]*))\"\s+error\=\"(?P<error>([^\"]*))\"\s+(reputation\=\"(?P<reputation_opt>([^\"]*))\"\s+)?category\=\"(?P<category>([^\"]*))\"\s+reputation\=\"(?P<reputation>([^\"]*))\"\s+categoryname\=\"(?P<categoryname>([^\"]*))\"\s+(content-type\=\"(?P<content_type>([^\"]*))\")?)?

以下是输入示例:

Oct 7 13:22:55 192.168.10.2 2013: 10:07-13:22:54 httpproxy[15359]: id="0001" severity="info" sys="SecureWeb" sub="http" name="http access" action="pass" method="GET" srcip="192.168.8.47" dstip="64.94.90.108" user="" statuscode="200" cached="0" profile="REF_DefaultHTTPProfile (Default Proxy)" filteraction="REF_DefaultHTTPCFFAction (Default content filter action)" size="1502" request="0x10870200" url="http://www.concordmonitor.com/csp/mediapool/sites/dt.common.streams.StreamServer.cls?STREAMOID=6rXcvJGqsPivgZ7qnO$Sic$daE2N3K4ZzOUsqbU5sYvZF78hLWDhaM8n_FuBV1yRWCsjLu883Ygn4B49Lvm9bPe2QeMKQdVeZmXF$9l$4uCZ8QDXhaHEp3rvzXRJFdy0KqPHLoMevcTLo3h8xh70Y6N_U_CryOsw6FTOdKL_jpQ-&CONTENTTYPE=image/jpeg" exceptions="" error="" category="134" reputation="neutral" categoryname="General News" content-type="image/jpeg"

我想把整个日志都拍下来

然而有时url中有许多引号,这使事情变得混乱。同样在一些日志中,error和信誉之间有一个额外的reputation数据字段。content-type也不总是出现。有时url数据字段之后的所有内容也会丢失。这就是为什么我添加了所有可选的?。我正在尝试考虑这些事件,并在必要时打印None。你知道吗


Tags: renoneidurlfieldtypeactionerror
1条回答
网友
1楼 · 发布于 2024-04-27 08:51:17

让我们把正则表达式分成两部分:

....url\=\"(?P<url>(.*))\"

以及

(\s+exceptions\=\"(?P<exceptions>([^\"]*))\"\s+error\=\"(?P<error>([^\"]*))\"\s+(reputation\=\"(?P<reputation_opt>([^\"]*))\"\s+)?category\=\"(?P<category>([^\"]*))\"\s+reputation\=\"(?P<reputation>([^\"]*))\"\s+categoryname\=\"(?P<categoryname>([^\"]*))\"\s+(content-type\=\"(?P<content_type>([^\"]*))\")?)?

第一部分中的.*是贪婪的。它将匹配所有它能匹配的,只有在绝对必要的情况下才回溯。你知道吗

第二部分是一个巨大的可选组。你知道吗

当regex执行时,.*将匹配字符串末尾的所有内容,然后根据需要回溯,直到\"可以匹配引号。这将是字符串中的最后一个引号,它可能不是您想要的引号。你知道吗

然后,巨可选组将尝试匹配,但是由于贪婪的.*已经吃掉了巨可选组应该解析的所有内容,它将失败。因为它是可选的,所以regex算法也可以。你知道吗

来解决这个问题?好吧,非贪婪量词可能有助于解决眼前的问题,但更好的解决方案可能是停止尝试使用正则表达式来解析这个问题。为您的数据格式查找现有的解析器。你想从HTML或XML中提取数据吗?我看到了很多关于BeautifulSoup的建议。你知道吗

相关问题 更多 >