gpt4 book ai didi

java - 提取键/值对,值可以跨行

转载 作者:行者123 更新时间:2023-11-30 06:44:10 25 4
gpt4 key购买 nike

输入文件:

key1=1
key2=start(a
b
c=
d)end
key3=d=e=f
somekey=start(123)end
morekey=start(1
2)end
key=jj

输出

key1    -> 1
key2 -> a
b
c=
d
key3 -> d=e=f
somekey -> 123
morekey -> 1
2
key -> jj

请求:在java中尝试。不能使用 java.util.Properties,正则表达式可以,但不是首选,更喜欢 StringUtils.substringBetween,但正则表达式可以。如何遍历多行并保留换行符。以下显然不适用于多行。打算尝试正则表达式,但前提是不可能有更优雅的方式。

    String[] str = line.split("=", 2);
StringUtils.substringBetween(line,startString,endString));

最佳答案

你的意思是这样的吗:

String str = "key1=1\n"
+ "key2=start(a\n"
+ "b\n"
+ "c=\n"
+ "d)end\n"
+ "key3=d=e=f\n"
+ "somekey=start(123)end\n"
+ "morekey=start(1\n"
+ "2)end\n"
+ "key=jj";
System.out.println(str.replaceAll("start\\(|\\)end", "")
.replaceAll("(\\w{2})=", "$1\t-> ")
.replaceAll("(\n\\w)", "\t$1"));

关于java - 提取键/值对,值可以跨行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43945523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com