re.search python regexpr

时间:2016-04-25 22:35:59

标签: python regex

对于我的作业我需​​要搜索一个程序并打印出每个用户打印的页数。

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'

1 个答案:

答案 0 :(得分:2)

描述

我建议你改用你的正则表达式,这样它会更灵活一些。这个正则表达式将执行以下操作:

  • 捕获用户名
  • 捕获打印数量
  • 允许用户和页面以任何顺序出现。如果您想开始捕获其他数据,这会变得很方便

正则表达式

Assume A
Prove that A => (BvA)
Assume B
Prove that B => (BvA)
So (AvB) => (BvA)     [That's v-intro, at least it is in Lemon's system which you appear to be using]

You've been given AvB. So modus ponens gives you BvA.

Regular expression visualization

<强>解释

^(?=.*?user:\s+(.*?)\s)(?=.*?pages:\s+(.*?)\s).*?$

实施例

正则表达式的在线演示

http://fiddle.re/13chna

Python代码示例

https://repl.it/CJdF/0

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"

示例输出

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