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.
['https://Stackoverflow.com/questions/17735661', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/713328/']
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)`](http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#group%28int%29) 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
}

```
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](http://rey.gimenez.biz/s/gpqqpzcn)
-----------------------------------------------

**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](http://rey.gimenez.biz/s/qz9s6fqb)
-----------------------------------------------