如何使用正则表达式拆分字符串并将字符串值放入Java中的映射中

时间:2021-03-15 21:40:20

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.

我有一个跟随字符串,我想用正则表达式读取它并作为键和值放入映射。我已经拆分并放入map.but问题是我使用了字符串数组并且有一个高数组索引的风险超出限制。所以我认为这种方式不适合良好的编码。

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

[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

1 个解决方案

#1


1  

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

你似乎有很多代码。以下是如何在1行中执行此操作:

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.

免责声明:未经测试或编译,只是翻阅过来。

#1


1  

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

你似乎有很多代码。以下是如何在1行中执行此操作:

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.

免责声明:未经测试或编译,只是翻阅过来。