Java 多行上的正则表达式
我有以下代码:Java 多行上的正则表达式,java,regex,Java,Regex,我有以下代码: Matcher matcher = Pattern.compile("<tag 1>(.*?)</tag 1>").matcher(buffer); int nr = 0; while (matcher.find()) { System.out.println("Match no. " + ++nr + ": '" + matcher.group() + "'"); } Matcher-Matcher=Pattern.c
Matcher matcher = Pattern.compile("<tag 1>(.*?)</tag 1>").matcher(buffer);
int nr = 0;
while (matcher.find()) {
System.out.println("Match no. " + ++nr + ": '" + matcher.group() + "'");
}
Matcher-Matcher=Pattern.compile((*?).Matcher(缓冲区);
int nr=0;
while(matcher.find()){
System.out.println(“匹配号“+++nr+”:““+matcher.group()+””);
}
其中缓冲区为:
<tag 1>
My Value
</tag 1>
我的价值
如何为正则表达式启用多行匹配,以便匹配此缓冲区?谢谢 您需要使用DOTALL标志以使点匹配换行符:
Matcher matcher = Pattern.compile("(?s)<tag 1>(.*?)</tag 1>").matcher(buffer);
Matcher-Matcher=Pattern.compile(“(?)(.*?)).Matcher(缓冲区);
否则:
Matcher matcher = Pattern.compile("<tag 1>(.*?)</tag 1>", Pattern.DOTALL)
Matcher Matcher=Pattern.compile((*?),Pattern.DOTALL)
但我要提醒大家,使用正则表达式解析HTML/XML并不是最好的主意。不要这样做。使用HTML或XML解析器。就解析器而言,jsoup()非常优秀,特别是如果您喜欢css选择器或使用
Pattern.compile((.*)、Pattern.DOTALL)