I have below content in text file
some texting content Test
I read it from
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.
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
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(<)
.