簡體   English   中英

Stream Api - 用逗號分割排除嵌套逗號

[英]Stream Api - split by comma exclude nested commas

我有下面的代碼,我在這里在線測試它。 也看過這個帖子 我不是最擅長正則表達式。 基本上我正在使用 stream API 吐出一個不包含內引號(雙/單)的字符串。 它只是一個永遠不會包含任何引號的純字符串。 我正在嘗試使用 stream API 將字符串按逗號拆分為鍵值對並將其放入 map。 但是,拆分也將requestparams=[id=4, isId=23]拆分為此isId=23], requestparams=[id=4 這一切都搞砸了。

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

class Main {
  public static void main(String[] args) {
    String dataPts = "requestparams=[id=4, isId=23], requestid=12345678, channel=BATCH";

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

    System.out.println(reconstructedMap);
  }
}

上述邏輯產生{ requestid=12345678, isId=23], requestparams=[id=4, channel=BATCH} [ ] 和里面的所有東西都應該保持原樣。 我應該看到{requestparams=[id=4, isId=23], requestid=12345678, channel=BATCH}

我想出的解決方案是使用正則表達式從字符串中完全剪掉 requestparams=[id=4, isId=23] ,如下面的方法所示。 然后將其添加回最后的 map。 它看起來很亂而且不尋常。

Map<String,String> convertDataPtsFromStringToMap(String myData{ 
    Pattern pattern = Pattern.compile("requestparams=(.*?])");//value alone [id=4, isId=23]
    Matcher matcher = pattern.matcher(myData);
    String reqParamsValue = "[]";

    if(matcher.find()) {
        reqParamsValue = matcher.group(1);
        myData = myData.replaceAll("(requestparams.*?]),", "");//replace requestparams=[id=4, isId=23]
    }

    Map<String, String> reconstructedMap = Arrays.stream(dataPts.split(",")).map(s -> s.split("=",2)).collect(Collectors.toMap(s -> s[0], s -> s[1]));
    reconstructedMap.put("requestparams", reqParamsValue);//add back the requestparams and its value to the resulting map
    return reconstructedMap;
}

requestparams將是唯一帶有 [ ] 的值並且不會更改,只是它必須在字符串中。 這就是為什么我可以將其剝離並放回原處。但是現在我的單元測試將要求該屬性始終位於字符串中,這不是問題。 這似乎太多了。 尋找更優雅的解決方案。

有沒有更好的方法使用 Streams-Api?

這是使用正式的 Java 正則表達式模式匹配器的一種方法:

String dataPts = "requestparams=[id=4, isId=23], requestid=12345678, channel=BATCH";
String pattern = "\\b([^=]+=(?:\\[[^]]+\\]|[^=,]+))(?:,\\s*|$)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(dataPts);
while (m.find()) {
    System.out.println(m.group(1));
}

這打印:

requestparams=[id=4, isId=23]
requestid=12345678
channel=BATCH

以下是正則表達式邏輯的解釋:

\b
(                  capture the following
    [^=]+          a LHS key
    =              =
    (?:
        \[[^]]+\]  either a RHS term in square brackets (check for this first)
        |          OR
        [^=,]+     another term not in brackets
    )
)                  end capture
(?:,\\s*|$)        then match either comma followed by whitespace, or the end of the string

如果您想使用鍵/值填充 map,請使用:

String dataPts = "requestparams=[id=4, isId=23], requestid=12345678, channel=BATCH";
Map<String, String> map = new HashMap<>();
String pattern = "\\b([^=]+)=(\\[[^]]+\\]|[^=,]+)(?:,\\s*|$)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(dataPts);
while (m.find()) {
    map.put(m.group(1), m.group(2));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM