繁体   English   中英

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

[英]Get substring of a string using regular expression in Java

我在使用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.

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

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

这给了我, Albedo–temperature feedbackSnowSmall-scale effects

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

尝试这个。

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());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM