简体   繁体   English

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

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

I am facing problem in extracting the substring from a string using regular expression in Java. 我在使用Java中的正则表达式从字符串中提取子字符串时遇到问题。 For example, I have the following piece of string. 例如,我有以下一串字符串。

===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.

Please note , the entire content is in a String. 请注意 ,整个内容都是字符串。

Here each element shown in between === are section headers and I want to extract each section content and its title (header). 这里===之间显示的每个元素都是节标题 ,我想提取每个节内容及其标题(标题)。

So, the output I am trying to generate looks like as follows. 所以,我试图生成的输出如下所示。

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.

I am using the following pattern definition to extract the headers. 我使用以下模式定义来提取标头。

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

This gives me, Albedo–temperature feedback , Snow , Small-scale effects . 这给了我, Albedo–temperature feedbackSnowSmall-scale effects

Now what I want is the content between each section headers. 现在我想要的是每个节标题之间的内容。 I am unable to extract them. 我无法提取它们。 Any help would be appreciated. 任何帮助,将不胜感激。

Try this. 尝试这个。

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