Ignoring the line break in regex?

前端 未结 3 1045
旧巷少年郎
旧巷少年郎 2021-01-13 03:04

I have below content in text file

  some texting content \"\"  Test

I read it from

相关标签:
3条回答
  • 2021-01-13 03:32

    You need to use Pattern.DOTALL mode.

    replaceAll() doesn't take mode flags as a separate argument, but you can enable them in the expression as follows:

    expectedString = inputString.replaceAll("(?s)\\<img.*?cid:part123.*?>", ...);
    

    Note, however, that it's not a good idea to parse HTML with regular expressions. It would be better to use HTML parser instead.

    0 讨论(0)
  • 2021-01-13 03:33

    By default, the . character will not match newline characters. You can enable this behavior by specifying the Pattern.DOTALL flag. In String.replaceAll(), you do this by attaching a (?s) to the front of your pattern:

    expectedString = inputString.replaceAll("(?s)\\<img.*?cid:part123.*?>", 
        "NewContent");
    

    See also Pattern.DOTALL with String.replaceAll

    0 讨论(0)
  • 2021-01-13 03:48

    If you want your dot (.) to match newline also, you can use Pattern.DOTALL flag. Alternativey, in case of String.replaceAll(), you can add a (?s) at the start of the pattern, which is equivalent to this flag.

    From the Pattern.DOTALL - 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.)

    So, you can modify your pattern like this: -

    expectedStr = inputString.replaceAll("(?s)<img.*?cid:part123.*?>", "Content");
    

    NOTE: - You don't need to escape your angular bracket(<).

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