本文介绍了Pattern.DOTALL与String.replaceAll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多行HTML文档,我试图从中获取一些东西。我正在使用java的正则表达式(我知道 - XML解析器bla bla bla,请跟我一起讨论:))。

I have a multiline HTML document that I am trying to get some stuff from. I'm using java's regex (I know - XML parsers bla bla bla, just bear with me here please :) ).

    dfahfadhadaaaa<object classid="java:com.sun.java.help.impl.JHSecondaryViewer" width="14" height="14">
<param name="content" value="../Glossary/glInterlinkedTask.html">

<param name="text" value="interlinked task">
<param name="viewerActivator" value="javax.help.LinkLabel">
<param name="viewerStyle" value="javax.help.Popup">
<param name="viewerSize" value="390,340">
<param name="textFontFamily" value="SansSerif">
<param name="textFontWeight" value="plain">
<param name="textFontStyle" value="italic">
<param name="textFontSize" value="12pt">
<param name="textColor" value="blue">

<param name=iconByID" value="">
</object>
sjtsjsrjrsjsrjsrj

我在字符串中输入了这个HTML:输入。

I've got this HTML in a string: input.

    input = input.replaceAll("<object classid=\"java:com.sun.java.help.impl.JHSecondaryViewer.*?object>", "buh bye!");

显然,它不起作用。但是,如果我将Pattern.compile与Pattern.DOTALL一起使用,我可以获得模式匹配。

Obviously, it's not working. HOWEVER, I can get a pattern match if I use pattern.compile with Pattern.DOTALL.

所以,我的问题是 - 我怎么能用string.replaceall做一些像Pattern.DOTALL的东西?

So, my question is - how can I do something like Pattern.DOTALL with string.replaceall?

推荐答案

(?s)附加到您的模式的前面:

Attach (?s) to the front of your pattern :

input = input.replaceAll("(?s)<object classid=\"java:com\\.sun\\.java\\.help\\.impl\\.JHSecondaryViewer.*?object>", "buh bye!");

来自:

其他标志也以这种方式工作

Other flags work this way as well

...

(?id msux-idmsux)没什么,但是开启匹配标志idmsux开 - 关

(?idmsux-idmsux) Nothing, but turns match flags i d m s u x on - off

在附注中,如果你的目标是从不受信任的来源中删除HTML中的不安全对象,请不要使用正则表达式,请不要标签。

On a side note, if your goal is to remove unsafe objects from HTML from an untrusted source, please don't use regular expressions, and please don't blacklist tags.

这篇关于Pattern.DOTALL与String.replaceAll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 00:37