[英]re.search python regexpr
對於我的作業,我需要搜索一個程序並打印出每個用戶已打印多少頁。
date: 2012-11-25
printer on time: 0800
23.96.82.161 user: pei printer: core 2 pages: 2 code: r n t h p r
28.104.177.80 user: isaac printer: poster pages: 4 code: p h
printer error: out of paper time: 1343
180.186.109.129 user: luis printer: core 2 pages: 2 code: k n h
194.96.54.184 user: isaac printer: sally pages: 6 code: p k r p f
122.230.32.236 user: luis printer: hill 3 pages: 8 code: n h n k q
printer off time: 2201
是程序將包含的示例,
for stringprint in logfile:
userRegex = re.search('(\suser:\s)(.+?)(\sprinter:\s)', stringprint)
if userRegex:
userString = userRegex.group(2)
numpages = int(re.search('(\spages:\s)(.+?)(\scode:\s)', stringprint).group(2))
if userString not in users:
user[userString] = numpages
else:
user[userString] += numpages
我的問題是re.search不能正常運行,我相信這種表達是正確的,但顯然不是。 我知道\\s
匹配空格, .+?
也匹配.+?
是與前面的令牌匹配的惰性版本。 一旦找到匹配項,我就使用user.Regex.group(2)
將其設置為“用戶名”。 然后,我要從那里搜索頁面數和代碼(以確保正確匹配),然后繼續打印。 我知道此正則表達式無法正常工作,但我無法弄清楚自己在做什么錯。
當我通過模塊運行程序時,我得到:
Traceback (most recent call last): File "C:\\Users\\brandon\\Desktop\\project3\\project3\\pages.py", line 45, in <module> log2hist("log") # version 2. File "C:\\Users\\brandon\\Desktop\\project3\\project3\\pages.py", line 29, in log2hist numpages = int(re.search('(\\spages:\\s)(.+?)(\\scode:\\s)',stringprint).group(2)) AttributeError: 'NoneType' object has no attribute 'group'
我建議切換您的Regex,這樣會更加靈活。 此正則表達式將執行以下操作:
正則表達式
^(?=.*?user:\s+(.*?)\s)(?=.*?pages:\s+(.*?)\s).*?$
解釋
NODE EXPLANATION
----------------------------------------------------------------------
^ the beginning of a "line"
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
----------------------------------------------------------------------
user: 'user:'
----------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
.*? any character except \n (0 or more
times (matching the least amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
----------------------------------------------------------------------
pages: 'pages:'
----------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
.*? any character except \n (0 or more
times (matching the least amount
possible))
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
----------------------------------------------------------------------
$ before an optional \n, and the end of a
"line"
正則表達式在線演示
樣本Python代碼
import re
SampleString = '''date: 2012-11-25
printer on time: 0800
23.96.82.161 user: pei printer: core 2 pages: 2 code: r n t h p r
28.104.177.80 user: isaac printer: poster pages: 4 code: p h
printer error: out of paper time: 1343
180.186.109.129 user: luis printer: core 2 pages: 2 code: k n h
194.96.54.184 user: isaac printer: sally pages: 6 code: p k r p f
122.230.32.236 user: luis printer: hill 3 pages: 8 code: n h n k q
printer off time: 2201'''
print (SampleString)
## Here re.findall()
Regex=re.compile(r'^(?=.*?user:\s+(.*?)\s)(?=.*?pages:\s+(.*?)\s).*?$',re.MULTILINE)
Matches = Regex.findall( SampleString)
Count = 0
for Match in Matches:
# do something with each found email string
print("[" + str(Count) + "][0] = " + Match[0])
print("[" + str(Count) + "][1] = " + Match[1])
print("")
Count = Count + 1
樣本輸出
[0][0] = pei
[0][1] = 2
[1][0] = isaac
[1][1] = 4
[2][0] = luis
[2][1] = 2
[3][0] = isaac
[3][1] = 6
[4][0] = luis
[4][1] = 8
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.