Java字符串 - 获取(但不包括)两个正则表达式之间的所有内容? [英] Java string - get everything between (but not including) two regular expressions?
问题描述
在Java中,是否有一种简单的方法可以通过在两侧指定正则表达式分隔符来提取子字符串,而不在最终子字符串中包含分隔符?
In Java, is there a simple way to extract a substring by specifying the regular expression delimiters on either side, without including the delimiters in the final substring?
For例如,如果我有这样的字符串:
For example, if I have a string like this:
<row><column>Header text</column></row>
提取子字符串的最简单方法是什么:
what is the easiest way to extract the substring:
Header text
请注意子字符串可能包含换行符...
Please note that the substring may contain line breaks...
谢谢!
推荐答案
写一个像这样的正则表达式:
Write a regex like this:
"(regex1)(.*)(regex2)"
...从匹配器中取出中间组(处理你想要使用的模式中的换行符 Pattern.DOTALL )。
... and pull out the middle group from the matcher (to handle newlines in your pattern you want to use Pattern.DOTALL).
使用您的示例,我们可以编写如下程序:
Using your example we can write a program like:
package test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
public static void main(String[] args) {
Pattern p = Pattern.compile(
"<row><column>(.*)</column></row>",
Pattern.DOTALL
);
Matcher matcher = p.matcher(
"<row><column>Header\n\n\ntext</column></row>"
);
if(matcher.matches()){
System.out.println(matcher.group(1));
}
}
}
哪个时候运行打印输出:
Which when run prints out:
Header
text
这篇关于Java字符串 - 获取(但不包括)两个正则表达式之间的所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!