gpt4 book ai didi

java - Java中使用正则表达式获取字符串的子字符串

转载 作者:太空宇宙 更新时间:2023-11-04 11:38:57 28 4
gpt4 key购买 nike

我在使用 Java 中的正则表达式从字符串中提取子字符串时遇到问题。例如,我有以下一段字符串。

===Albedo–temperature feedback===
When an area's albedo changes due to snowfall, a snow–temperature [[feedback]] results.

===Snow===
Snow albedo is highly variable, ranging from as high as 0.9 for freshly fallen snow, to about 0.4 for melting snow, and as low as 0.2 for dirty snow.

====Small-scale effects====
Albedo works on a smaller scale, too.

请注意,整个内容都在一个字符串中。

这里 === 之间显示的每个元素都是节标题,我想提取每个节内容及其标题(标题)。

因此,我尝试生成的输出如下所示。

1. Albedo–temperature feedback
content: When an area's albedo changes due to snowfall, a snow–temperature [[feedback]] results.

2. Snow
content: Snow albedo is highly variable, ranging from as high as 0.9 for freshly fallen snow, to about 0.4 for melting snow, and as low as 0.2 for dirty snow.

2. Small-scale effects
content: Albedo works on a smaller scale, too.

我使用以下模式定义来提取 header 。

Pattern pattern = Pattern.compile("[=]{2,5}(.*?)[=]{2,5}");

这给了我,反照率-温度反馈小规模效果

现在我想要的是每个节标题之间的内容。我无法提取它们。任何帮助将不胜感激。

最佳答案

试试这个。

String s = ""
+ "===Albedo–temperature feedback===\n"
+ "When an area's albedo changes due to snowfall, a snow–temperature [[feedback]] results. \n"
+ "\n"
+ "===Snow===\n"
+ "Snow albedo is highly variable, ranging from as high as 0.9 for freshly fallen snow, to about 0.4 for melting snow, and as low as 0.2 for dirty snow.\n"
+ "\n"
+ "====Small-scale effects====\n"
+ "Albedo works on a smaller scale, too.\n";
Pattern PAT = Pattern.compile("^()$|^={2,5}(.+?)={2,5}$|^(.+)$", Pattern.MULTILINE);
String NEWLINE = "\n";
Matcher m = PAT.matcher(s);
int number = 0;
StringBuilder sb = new StringBuilder();
while (m.find()) {
if (m.group(2) != null)
sb.append(++number).append(". ").append(m.group(2));
else if (m.group(3) != null)
sb.append("content: ").append(m.group(3));
sb.append(NEWLINE);
}
System.out.println(sb.toString());

关于java - Java中使用正则表达式获取字符串的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42986772/

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