Pattern.DOTALL with String.replaceAll

前端 未结 1 1615
有刺的猬
有刺的猬 2020-12-10 11:20

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 :) ).

1条回答
  •  遥遥无期
    2020-12-10 11:33

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

    input = input.replaceAll("(?s)", "buh bye!");
    
    

    From the Javadoc:

    Dotall mode can also be enabled via the embedded flag expression (?s). (The s is a mnemonic for "single-line" mode, which is what this is called in Perl.)

    Other flags work this way as well

    Special constructs (non-capturing)

    ...

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

    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.

    0 讨论(0)
    提交回复
    热议问题