I have a following String and i want to read it using regular expression and put into a map as a key and value.I have already split and put into a map.but the problem is that i have used string arrays and there is a high risk of array index out of bound.so i think that way is not suit for good coding.
public static void read(String log,Map<String, String> logMap) {
String sanitizeLog = "";
String commaSeparatedLine[];
String equalSeparatedLine[];
String patternComma = ",";
String patternEqual = "=";
String patternSanitize = "(?<=]:).*";
Pattern pattern = Pattern.compile(patternSanitize);
Matcher matcher = pattern.matcher(log);
if (matcher.find()) {
sanitizeLog = matcher.group();
}
pattern = Pattern.compile(patternComma);
commaSeparatedLine = pattern.split(sanitizeLog);
for (String line : commaSeparatedLine) {
pattern = Pattern.compile(patternEqual);
equalSeparatedLine = pattern.split(line);
for (int i = 0; i < equalSeparatedLine.length; i += 2) {
logMap.put(equalSeparatedLine[i].trim(),
equalSeparatedLine[i + 1]);
}
}
}
Above code snippet is working fine.but there i used lot of string arrays to store split values.Please let me know that is there any way to do the same thing without using string arrays and put split values in to a map using regular expression.I am a newbie in regular expression.
Output Map should contain like this.
Key -> value
DB.UPDATE_CT -> 2
DB.DUPQ_CT -> 1
...
String value to be split
[2015-01-07 07:17:56,911]: R="InProgressOrders.jsp", REQUEST_UUID="77ed2ab1-b799-4715-acd5-e77ab756192e", HTTP_M="POST", PFWD="login.jsp", USER_ORG="TradeCustomer.1717989", TX_ORG1="1717989", DB.QUERY_CT=61, DB.UPDATE_CT=2, DB.DUPQ_CT=1, DB.SVR_MS=59, DB.IO_MS=111, DB.DRV_MS=144, DB.LOCK_MS=31, DB.BYTES_W=1501, KV.PUT=1, KV.GET=5, KV.PWAIT_MS=2, KV.GWAIT_MS=4, KV.BYTES_W=193, KV.BYTES_R=367, MCACHE.GET=30, MCACHE.PUT=18, MCACHE.L1HIT=10, MCACHE.L2HIT=1, MCACHE.HIT=1, MCACHE.MISS=18, MCACHE.WAIT_MS=51, MCACHE.BYTES_W=24538, MCACHE.BYTES_R=24282, ROOTS.READ_CT=6, ROOTS.DUPRSV_CT=3, THREAD.WALL_MS=594, THREAD.CPU_MS=306, THREAD.CPU_USER_MS=300, THREAD.MEM_K=19318