ashwinsakthi
ashwinsakthi

Reputation: 1956

Regex for getting data within braces in java

I need a proper regex more specifically in Java to get strings between [].

String:

[2015-04-09 13:10:27,858] [1428599427721] [{[email protected]}{SpringFramework}{Host123}{58}{20150409131026660}][getfilesInput] [WebContainer : 2] 

I need to print in the below format:

2015-04-09 13:10:27,858
1428599427721
[email protected]
etc..

Tried the regular expression \\[(.*?)\\] as shown below, but it is not working.

Pattern p = Pattern.compile("\\[(.*?)\\]");

Upvotes: 0

Views: 76

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626747

This code will correctly extract all those values inside []s and {}s:

String str = "[2015-04-09 13:10:27,858] [1428599427721] [{[email protected]}{SpringFramework}{Host123}{58}{20150409131026660}][getfilesInput] [WebContainer : 2]";
String rx = "[\\[{]+([^\\]}]*?)[\\]}]+";
Pattern ptrn = Pattern.compile(rx);
Matcher m = ptrn.matcher(str);
while (m.find()) {
   System.out.println(m.group(1));
}

Output:

2015-04-09 13:10:27,858                                                                                                                                                                                                                                
1428599427721                                                                                                                                                                                                                                          
[email protected]                                                                                                                                                                                                                                 
SpringFramework                                                                                                                                                                                                                                        
Host123                                                                                                                                                                                                                                                
58                                                                                                                                                                                                                                                     
20150409131026660                                                                                                                                                                                                                                      
getfilesInput                                                                                                                                                                                                                                          
WebContainer : 2

EDIT

The main problem with the original regex is that it only captures what is inside square brackets (see demo), while we need to match everything in-between curly braces and square brackets.

Upvotes: 1

Related Questions