文本介于<;预处理>;使用正则表达式Java时,标记不保留换行符

文本介于<;预处理>;使用正则表达式Java时,标记不保留换行符,java,regex,Java,Regex,这是我的问题 String pattern1 = "<pre.*?>(.+?)</pre>"; Matcher m = Pattern.compile(pattern1).matcher(html); if(m.find()) { String temp = m.group(1); System.out.println(temp); } String pattern1=“(.+?)”; Matcher m=Pattern.compile(pattern1

这是我的问题

String pattern1 = "<pre.*?>(.+?)</pre>";
Matcher m = Pattern.compile(pattern1).matcher(html);
if(m.find()) {
    String temp = m.group(1);
    System.out.println(temp);
}
String pattern1=“(.+?)”;
Matcher m=Pattern.compile(pattern1.Matcher)(html);
if(m.find()){
字符串温度=m组(1);
系统输出打印项次(温度);
}

temp不保留换行符…它作为一条单行流动。如何在temp中保留换行符?

您不应该使用正则表达式解析HTML,但要解决此问题,请使用修饰符

String pattern1 = "(?s)<pre[^>]*>(.+?)</pre>";
                   ↑↑↑↑
                     |_______ Forces the . to span across newline sequences.
String pattern1=“(?s)]*>(.+?)”;
↑↑↑↑
|_______强制执行。跨越换行序列。

不应使用正则表达式解析HTML,但要解决此问题,请使用修饰符

String pattern1 = "(?s)<pre[^>]*>(.+?)</pre>";
                   ↑↑↑↑
                     |_______ Forces the . to span across newline sequences.
String pattern1=“(?s)]*>(.+?)”;
↑↑↑↑
|_______强制执行。跨越换行序列。
使用JSoup:html解析器 众所周知,您不应该使用正则表达式来解析html内容,而应该使用html解析器。您可以在下面看到如何使用JSoup执行此操作:

String html = "<p>lorem ipsum</p><pre>Hello World</pre><p>dolor sit amet</p>";
Document document = Jsoup.parse(html);
Elements pres = document.select("pre");

for (Element pre : pres) {
    System.out.println(pre.text());
}
"; Matcher m=Pattern.compile(pattern1,Pattern.DOTALL).Matcher(html); if(m.find()){ 字符串温度=m组(1); 系统输出打印项次(温度); } 内联单行标志: 或者在正则表达式中内联使用
s
标志,如下所示:

String pattern1 = "<pre.*?>([\\s\\S]+?)</pre>";
Matcher m = Pattern.compile(pattern1).matcher(html);
if(m.find()) {
    String temp = m.group(1);
    System.out.println(temp);
}
"; Matcher m=Pattern.compile(pattern1.Matcher)(html); if(m.find()){ 字符串温度=m组(1); 系统输出打印项次(温度); } 但是正如在他的评论中指出的,这个技巧可能会影响正则表达式引擎的性能。

使用JSoup:html解析器 众所周知,您不应该使用正则表达式来解析html内容,而应该使用html解析器。您可以在下面看到如何使用JSoup执行此操作:

String html = "<p>lorem ipsum</p><pre>Hello World</pre><p>dolor sit amet</p>";
Document document = Jsoup.parse(html);
Elements pres = document.select("pre");

for (Element pre : pres) {
    System.out.println(pre.text());
}
"; Matcher m=Pattern.compile(pattern1,Pattern.DOTALL).Matcher(html); if(m.find()){ 字符串温度=m组(1); 系统输出打印项次(温度); } 内联单行标志: 或者在正则表达式中内联使用
s
标志,如下所示:

String pattern1 = "<pre.*?>([\\s\\S]+?)</pre>";
Matcher m = Pattern.compile(pattern1).matcher(html);
if(m.find()) {
    String temp = m.group(1);
    System.out.println(temp);
}
"; Matcher m=Pattern.compile(pattern1.Matcher)(html); if(m.find()){ 字符串温度=m组(1); 系统输出打印项次(温度); }

但是正如在他的评论中指出的,这个技巧可能会影响正则表达式引擎的性能。

我相信您想将
模式.DOTALL
添加到
编译
中。仅供参考,如果
测试冗余,那么您的第二个
。如果
m.find()
通过,您可以指望
m.group(1)
为非空且至少有一个字符长。谢谢..将进行更改..我相信您希望将
Pattern.DOTALL
添加到
编译
中。仅供参考,您的第二个
If
测试是多余的。如果
m.find()
通过,您可以指望
m.group(1)
非空且至少有一个字符长。谢谢..将进行更改..您想做什么的问题不清楚,dotall修饰符允许您跨换行匹配(保留它们)…我试图从站点获取Whois数据…我试图只解析原始Whois数据…DOTALL似乎与前文本中的换行符不匹配。域名:1800reservation.COM注册域名:313310477\u Domain\u COM-vrsnregistrator等等……即使它对我不起作用……我学到了一些新东西。1向上投票。您想做什么的问题不清楚,dotall修饰符允许您跨换行符进行匹配(保留换行符)…我试图从站点获取Whois数据…我试图仅解析原始Whois数据…dotall似乎与前文本中的换行符不匹配。域名:1800reservation.COM注册域名:313310477\u Domain\u COM-vrsnregistrator等等……即使它对我不起作用……我学到了一些新东西。1向上投票。虽然有效,但不要在Java中使用正则表达式技巧
[\s\s]
。速度慢了。@nhahdh指针不错,我会把你的评论添加到答案中。谢谢正如@Fede所提到的,我使用了Jsoup解析器……html中只有一个pre标记……但是换行符并不存在。可能没有…但是pre中的文本在源代码中已格式化…虽然它是有效的,但不要在Java中使用正则表达式技巧
[\s\s]
。速度慢了。@nhahdh指针不错,我会把你的评论添加到答案中。谢谢正如@Fede所提到的,我使用了Jsoup解析器……html中只有一个pre标记……但是换行符并不存在。可能没有…但是pre中的文本在源代码中格式化。。。