I have below content in text file
some texting content <img src="cid:part123" alt=""> <b> Test</b>
I read it from file and store it in String i.e inputString
expectedString = inputString.replaceAll("\\<img.*?cid:part123.*?>",
"NewContent");
I get expected output i.e
some texting content NewContent <b> Test</b>
Basically if there is end of line character in between img and src like below, it does not work for example below
<img
src="cid:part123" alt="">
Is there a way regex ignore end of line character in between while matching?
If you want your
dot (.)to matchnewlinealso, you can usePattern.DOTALLflag. Alternativey, in case ofString.replaceAll(), you can add a(?s)at the start of the pattern, which is equivalent to this flag.From the
Pattern.DOTALL– JavaDoc : –So, you can modify your pattern like this: –
NOTE: – You don’t need to escape your
angular bracket(<).