Natta
Natta

Reputation: 755

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?

Upvotes: 0

Views: 111

Answers (3)

Paul Alexander
Paul Alexander

Reputation: 32367

If your implementation supports named groups...

/ErrorID:\s+(?<ID>.*)\nTime:\s+(?<Time>.*)\nURL:\s+(?<URL>.*)/g

You can then reference them by name.

Otherwise by index

/ErrorID:\s+(.*)\nTime:\s+(.*)\nURL:\s+(.*)/g

$1 for ID, $2 for Time and $3 for URL.

Upvotes: 0

user557597
user557597

Reputation:

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:)

Upvotes: 0

Gabi Purcaru
Gabi Purcaru

Reputation: 31524

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.

Upvotes: 1

Related Questions