-1

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

Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
gihan-maduranga
  • 4,381
  • 5
  • 41
  • 74
  • possible duplicate of [How to split a string in Java](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – MaximeF Sep 04 '15 at 15:21
  • 1
    `"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."` -- It would be much easier to help you fix and improve your code if you included it as part of your question. – azurefrog Sep 04 '15 at 15:35
  • This is the pattern you are looking for: `([A-Z_.]*)="([^"]*)"`. – Tunaki Sep 04 '15 at 15:35
  • 1
    I'm pretty sure this solves op's problem: pastebin.com/TWdJvsNK But if you want to grab the strings between the quotes and not the actual quotes you can use this regex instead: "([\\w\\.]+)=\\\"?([\\w\\d\\.-]+)\\\"?" – Pedro Pinheiro Sep 04 '15 at 16:46
  • @ Pedro Pinheiro Thanks a lot for your answer.It is working fine.If you can please include your answer here.so it will help for lot of people if they need.even i can accept it as valid answer :) – gihan-maduranga Sep 07 '15 at 05:46

1 Answers1

1

You seem to have a lot of code. Here is how to do it in 1-line:

Map<String, String> map = Arrays.stream(input.split(","))
  .map(s -> a.split("="))
  .collect(Collectors.toMap(a -> a[0], a -> a[1]));

To instead add the entries to another map (as in your code):

Arrays.stream(input.split(",")).map(s -> a.split("="))
    .forEach(a -> logMap.put(a[0], a[1]));

Disclaimer: Not tested or compiled, just thumbed in.

Bohemian
  • 412,405
  • 93
  • 575
  • 722