为什么Python正则表达式通配符只匹配换行符

为什么Python正则表达式通配符只匹配换行符,python,regex,wildcard,logfile,Python,Regex,Wildcard,Logfile,我正在编写一个程序,使用Python正则表达式解析日志消息。我已经把一切都安排好了,直到日志上的信息。这可能是任意数量的字符类型,因此我假设。*通配符是解决此问题的最佳方案。除了一条新的线路外,它与任何东西都匹配 然而,当我使用通配符时,在这个实例中唯一返回的是新行。有什么想法吗?以下是代码和输出: import os import re #Change to and print correct file path os.chdir('/Users/MacUser/Desktop/regExPy

我正在编写一个程序,使用Python正则表达式解析日志消息。我已经把一切都安排好了,直到日志上的信息。这可能是任意数量的字符类型,因此我假设。*通配符是解决此问题的最佳方案。除了一条新的线路外,它与任何东西都匹配

然而,当我使用通配符时,在这个实例中唯一返回的是新行。有什么想法吗?以下是代码和输出:

import os
import re
#Change to and print correct file path
os.chdir('/Users/MacUser/Desktop/regExPython')
print(os.getcwd())

#Iterate and read from syslogexample.txt then print results
line_number = 0
with open('syslogexample.txt', 'r') as syslog:
    log_lines = syslog.readlines()
    for line in log_lines:
        line_number += 1
        print('{:>4} {}'.format(line_number, line.rstrip()))


#Build regex to parse through the data
DATE_RE = r'(\w{3}\s+\d{2})'
TIME_RE = r'(\d{2}:\d{2}:\d{2})'
DEVICE_RE = r'(\S+)'
PROCESS_RE = r'(\S+\s+\S+:)'
MESSAGE_RE = r'(.*)'
CD_RE = r'(\s+)'

Syslog_RE = DATE_RE + CD_RE + \
            TIME_RE + CD_RE + \
            DEVICE_RE + CD_RE + \
            PROCESS_RE + CD_RE + \
            MESSAGE_RE

#Use regex to parse through the data
for line in log_lines:
    m = re.match(Syslog_RE, line)
    if m:
        print(m.groups())

#Printed log Files
      1 apr 29 08:22:13 mac-users-macbook-8 syslogd[49]: asl sender statistics
   2 apr 29 08:22:17 mac-users-macbook-8 com.apple.xpc.launchd[1] (com.apple.xpc.launchd.domain.system):
   3 service "com.apple.emond.aslmanager" tried to hijack endpoint "com.apple.aslmanager" from owner:
   4 com.apple.aslmanager
   5 apr 29 08:22:17 mac-users-macbook-8 com.apple.xpc.launchd[1] (com.apple.xpc.launchd.domain.system):
   6 service "com.apple.emond.aslmanager" tried to hijack endpoint
   7 "com.apple.activity_tracing.cache-delete" from owner: com.apple.aslmanager
   8 apr 29 08:22:17 mac-users-macbook-8 com.apple.xpc.launchd[1] (com.apple.bsd.dirhelper[14184]):
   9 endpoint has been activated through legacy launch(3) apis. please switch to xpc or
  10 bootstrap_check_in(): com.apple.bsd.dirhelper
  11 apr 29 08:22:19 mac-users-macbook-8 com.apple.xpc.launchd[1]
  12 (com.apple.imfoundation.imremoteurlconnectionagent): unknown key for integer:
  13 _dirtyjetsammemorylimit

Parsed Log Files
('apr 29', ' ', '08:22:17', ' ', 'mac-users-macbook-8', ' ', 'com.apple.xpc.launchd[1] (com.apple.xpc.launchd.domain.system):', '\n', '')
('apr 29', ' ', '08:22:17', ' ', 'mac-users-macbook-8', ' ', 'com.apple.xpc.launchd[1] (com.apple.xpc.launchd.domain.system):', '\n', '')
('apr 29', ' ', '08:22:17', ' ', 'mac-users-macbook-8', ' ', 'com.apple.xpc.launchd[1] (com.apple.bsd.dirhelper[14184]):', '\n', '')

Process finished with exit code 0
正如您在末尾看到的,MESSAGE\u RE是唯一打印的字符,是我认为根本不会打印的换行符


谢谢大家

正则表达式中的无法正常工作,因为
*
只捕获换行符,这意味着在换行符处,即从第3行到第4行,它停止匹配。也许可以尝试
re.compile()
并在
re.match()
之前编译正则表达式。在python正则表达式模块中,有一个
DOTALL
标志,该标志允许正则表达式中的
也匹配换行符

,因为
*
只捕获换行符,这意味着在换行符(即第3行到第4行)处停止匹配。也许可以尝试
re.compile()
并在
re.match()
之前编译正则表达式。在python正则表达式模块中,有一个
DOTALL
标志,它允许
也匹配换行符

'\n'
s与CD\u RE匹配,消息总是生成
'
,因为行中没有剩余内容。由于您一次只查看一行,并且消息总是在单独的行上,因此消息不可能匹配任何内容。问题是,每个类似日志的内容可以拆分为任意数量的文本行。这很奇怪,因为在我所知道的每一个日志系统中(包括苹果),每行总是有一条消息。如果您能够以原始形式获取日志,那么这是最干净的解决方案。如果您确实需要对正则表达式进行多行匹配,请查看
'\n'
s是CD\u RE的匹配项,消息\u RE总是生成
'
,因为行中没有剩余内容。由于您一次只查看一行,并且消息总是在单独的行上,因此消息不可能匹配任何内容。问题是,每个类似日志的内容可以拆分为任意数量的文本行。这很奇怪,因为在我所知道的每一个日志系统中(包括苹果),每行总是有一条消息。如果您能够以原始形式获取日志,那么这是最干净的解决方案。如果您确实需要对正则表达式进行多行匹配,请查看