1

I need to parse log files and get some values to variable. The log file will have a string

String logStr = "21:19:03 -[ 8b4]- ERROR - Jhy AlarmOccure::OnAdd - Updated existing alarm: ID [StrValue1:StrValu2|StrValue3], Instance [4053], SetStatus [0], AckStatus [1], SetTime [DateValue4], ClearedTime [DateValue5]";

I need to get StrValue1,StrValue2,StrValue3,DateValue4 and DateValue5 to varaibles these values are changing fields when ever there is an error.

First i was trying to at least get StrValue1. But not getting the expected result.

Pattern twsPattern = Pattern.compile(".*?ID ?[([^]:]*):([^]|]*)|([^]]*)]");//.*ID\\s$.([^]:]*.):.([^]|]*.)|.([^]]*.).]
Matcher twsMatcher = twsPattern.matcher(logStr);
if(twsMatcher.find()){
    System.out.println(twsMatcher.start());
    System.out.println(twsMatcher.group());
    System.out.println(twsMatcher.end());
}

I am not able to understand the grouping stuff, in regex.

4 Answers 4

2

Try regexp ([a-zA-z]+) \[([^\]]+)\].

For string 21:19:03 -[ 8b4]- ERROR - Jhy AlarmOccure::OnAdd - Updated existing alarm: ID [StrValue1:StrValu2|StrValue3], Instance [4053], SetStatus [0], AckStatus [1], SetTime [DateValue4], ClearedTime [DateValue5] it returns:

  • ID and StrValue1:StrValu2|StrValue3
  • Instance and 4053
  • SetStatus and 0
  • AckStatus and 1
  • SetTime and DateValue4
  • ClearedTime and DateValue5

You can test it here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer... Since the strings are coming from log files by an application these will be constant..
2

Good on you for the attempt! You're actually doing quite well. You need to escape square brackets that you don't mean as character classes, i.e.

.*?ID ?\[
       ^

And hopefully you are aware that by ([^]:]*) you are meaning, "The longest possible string of characters without a closing square bracket or colon."

You probably also want to escape the |, as that is an alternation operator in regular expressions, i.e.

\|

Comments

2

Long story short, your regex lacks escaping some chars, like [ and | (this one, if outside a character class - []).

So when you want to actually match the [ char, you have to use \[ (or \\[ inside the java string). Also, the negation in the group ([^]:]*) is not what it seems. You probably want just ([^:]*), which matches everything until a :.

To make it work, then, you would simply use Matcher#group(int) to retrieve the values. This is the adapted code with the final regex:

String logStr = "21:19:03 -[ 8b4]- ERROR - Jhy AlarmOccure::OnAdd - Updated existing alarm: ID [StrValue1:StrValu2|StrValue3], Instance [4053], SetStatus [0], AckStatus [1], SetTime [DateValue4], ClearedTime [DateValue5]";
Pattern twsPattern = Pattern.compile(".*?ID ?\\[([^:]*):([^|]*)\\|([^\\]]*)\\].*?SetTime ?\\[([^\\]]*)\\][^\\[]+\\[([^\\]]*)\\]");
Matcher twsMatcher = twsPattern.matcher(logStr);
if (twsMatcher.find()){
    System.out.println(twsMatcher.group(1)); // StrValue1
    System.out.println(twsMatcher.group(2)); // StrValu2
    System.out.println(twsMatcher.group(3)); // StrValue3
    System.out.println(twsMatcher.group(4)); // DateValue4
    System.out.println(twsMatcher.group(5)); // DateValue5
}

Comments

0

I like more general solutions, but here is a very specific pattern you can use if it suits you. It will capture all of the values in a string as long as they are follow the same, very specific pattern.

ID (?:\[([^\]:]+):([^\]|]+)\|([^\]]+)\]).*?SetTime \[([^\]]+)\], ClearedTime \[([^\]]+)\]

Here is the result:

1: ID [StrValue1:StrValu2|StrValue3], Instance [4053], SetStatus [0], AckStatus [1], SetTime [DateValue4], ClearedTime [DateValue5]
  [1]: StrValue1
  [2]: StrValu2
  [3]: StrValue3
  [4]: DateValue4
  [5]: DateValue5

Try it out

Multiple Matches per line

This version will just match each instance in a string of ID, SetTime, or ClearedTime followed by a bracketed value.

(ID|SetTime|ClearedTime) \[([^\]]+)\

Results

1: ID [StrValue1:StrValu2|StrValue3]
  [1]: ID
  [2]: StrValue1:StrValu2|StrValue3
1: SetTime [DateValue4]
  [1]: SetTime
  [2]: DateValue4
1: ClearedTime [DateValue5]
  [1]: ClearedTime
  [2]: DateValue5

Try it out

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.