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

时间:2017-03-23 20:53:18

标签: java string

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

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

1 个答案:

答案 0 :(得分:0)

试试这个。

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