Python 如何找到两个图案之间的输出线(它们相同)
我试图解析一些日志文件,每行都以时间戳开头,例如: [11/16/18 16:40:04:097东部标准时间] 如果日志中没有任何错误,那么每一行都将具有相同的起始模式。但是,如果发生某些错误,则整个错误堆栈将打印时间戳,如下所示:Python 如何找到两个图案之间的输出线(它们相同),python,regex,Python,Regex,我试图解析一些日志文件,每行都以时间戳开头,例如: [11/16/18 16:40:04:097东部标准时间] 如果日志中没有任何错误,那么每一行都将具有相同的起始模式。但是,如果发生某些错误,则整个错误堆栈将打印时间戳,如下所示: [11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL
[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName
Additional Data:
null
Current exception:
Message:
_ERR_BSAFE_FUNCTION
Stack trace:
我想做的是增加整个错误堆栈,例如,输入是:
[11/16/18 16:40:04:098 EST] 000000ae CommandLogger 2 PerfLog <entry operation="Command : com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl" parameters="@releaseID=9.0
[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName
Additional Data:
null
Current exception:
Message:
_ERR_BSAFE_FUNCTION
Stack trace:
[11/16/18 16:40:04:101 EST] 000000ae SystemErr R
[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.
at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)
尝试了以下内容但失败了,如果你能让我知道我的代码出了什么问题,那就太好了
import re, sys
if len(sys.argv) > 1:
with open(sys.argv[1]) as f:
text = f.read()
else:
text = sys.stdin.read()
p_start = r'^\[\d{2}/.*'
p_end = r'^\[\d{2}/.*'
pattern = r'{p0}(?!.*{p0})(?:.*?{p1}|.*)'.format(p0=p_start, p1=p_end)
error_no_match = 'No Match'
matches = re.findall(pattern, text, flags=re.M|re.DOTALL)
if matches:
for match in matches:
print 'match:', match
print len(matches)
else:
print error_no_match
将整个文件读入变量
text
,可以使用
matches = re.findall(r'^\[\d{2}/.*(?:\n(?!\[\d{2}/).*)+', text, re.M)
看。请注意,如果文本包含CRLF结尾,则需要将\n
替换为\r?\n
(其中CR是可选的)
详细信息
修饰符使re.M
在行首匹配^
-行的开头^
-a\[
字符[
-2位数字和一个\d{2}/
字符/
-行的其余部分*
-一次或多次重复(?:\n(?!\[\d{2}/).+
-一个LF符号(如果可能有CRLF结尾,请使用\n(?!\[\d{2}/)
),后面不跟\r?\n
和两位数字和[
//code>
-行的其余部分*
将整个文件读入变量
text
,可以使用
matches = re.findall(r'^\[\d{2}/.*(?:\n(?!\[\d{2}/).*)+', text, re.M)
请参阅。注意,如果文本包含CRLF结尾,则需要将\n
替换为\r?\n
(其中CR是可选的)
详细信息
修饰符使re.M
在行首匹配^
-行的开头^
-a\[
字符[
-2位数字和一个\d{2}/
字符/
-行的其余部分*
-一次或多次重复(?:\n(?!\[\d{2}/).+
-一个LF符号(如果可能有CRLF结尾,请使用\n(?!\[\d{2}/)
),后面不跟\r?\n
和两位数字和[
//code>
-行的其余部分*
您似乎需要
re.findall(r'(?m)^\[\d{2}/*(?:\n(?)[\d{2}/).+',text)
,请参阅。您似乎需要re.findall(r'(?m)^\[\d{2}/*(?:\n(?![\d{2}/).+',text)
,请参阅。谢谢您的帮助,Wiktor。我很难弄清楚![\d{2}/.[\n}}+“我自己…你知道我哪里弄错了吗…@IanZhang这是因为你试图在两者之间匹配一些东西,而在这里更容易得到所有不以某个模式开头的连续行,并且结尾处的量词非常重要。*(?:\n(?![\d{2}/).+
,[
没有转义。您也不匹配起始模式,尽管这是次要的。另外,感谢,for+,它是aginst.*(:\n(?)[\d{2}/)*)或整个模式^[\d{2}/*(?:\n(?)[\d{2}/)*)。此外,您还被以下内容弄糊涂了:您能解释一下吗?@IanZhang(?:)
是一个,仅用于将多个模式分组为一系列模式,您可以量化或使用多个备选方案。我的模式中的最后一个+
确保正则表达式只匹配多行条目。感谢您的帮助,Wiktor。我很难弄清楚。*(?::(![\d{2}/)*)+“我自己…你知道我哪里弄错了吗…@IanZhang这是因为你试图在两者之间匹配一些东西,而在这里更容易得到所有不以某个模式开头的连续行,并且结尾处的量词非常重要。*(?:\n(?![\d{2}/).+
,[
没有转义。您也不匹配起始模式,尽管这是次要的。另外,感谢,for+,它是aginst.*(:\n(?)[\d{2}/)*)或整个模式^[\d{2}/*(?:\n(?)[\d{2}/)*)。此外,您还被以下内容弄糊涂了:您能解释一下吗?@IanZhang(?:)
是一个,仅用于将多个模式分组为一系列模式,您可以量化或使用多个备选方案。我的模式中的最后一个+
确保正则表达式只匹配多行条目。
import re
rx = r"^\[\d{2}/.*(?:\n(?!\[\d{2}/).*)+"
text = "[11/16/18 16:40:04:098 EST] 000000ae CommandLogger 2 PerfLog <entry operation=\"Command : com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl\" parameters=\"@releaseID=9.0 \n[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName \nAdditional Data: \n null\nCurrent exception:\nMessage:\n_ERR_BSAFE_FUNCTION\nStack trace:\n[11/16/18 16:40:04:101 EST] 000000ae SystemErr R \n[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.\n at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)"
matches = re.findall(rx, text, re.M)
print(matches)
[
'[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName \nAdditional Data: \n null\nCurrent exception:\nMessage:\n_ERR_BSAFE_FUNCTION\nStack trace:',
'[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.\n at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)'
]