Using regular expressions for my error log in Java
I'm trying to use regular expressions to parse text like this:
'''ErrorID: 951574305
Time: Mon Apr 25 16:01:34 CEST 2011
URL: /documents.do
HttpCode: null
Error: class java.lang.NullPointerException: null'''
Where keywords ErrorID: , Time: , URL: are always the 开发者_运维百科same and I need to search for them. How do I parse this text?
import re
re.findall("ErrorID:\s+(.*)", text)
# ['951574305']
re.findall("Time:\s+(.*)", text)
# ['Mon Apr 25 16:01:34 CEST 2011']
re.findall("URL:\s+(.*)", text)
# ['/documents.do']
The regex works this way: it matches on ErrorID:
(or other delimiter) plus some spaces, plus the rest of the string until the newline/end of string. Then it returns that "something" after the whitespace. Also, the result will be a list in which you will need the first item.
There can be other strategies of finding what you need, but I found this the most appropriate.
If your implementation supports named groups...
/ErrorID:\s+(?<ID>.*)\nTime:\s+(?<Time>.*)\nURL:\s+(?<URL>.*)/g
You can then reference them by name. See at http://refiddle.com/10w
Otherwise by index
/ErrorID:\s+(.*)\nTime:\s+(.*)\nURL:\s+(.*)/g
$1 for ID, $2 for Time and $3 for URL. http://refiddle.com/10x
This is not the answer you are looking for, but here are two sites that you might find helpful:
java regex tester
java Script regex tester
If you require all of these in the string and don't know where they are and can use lookahead assertions:
(?=[\S\s]*ErrorID:)(?=[\S\s]*Time:)(?=[\S\s]*URL:)
精彩评论