I have below message String. I want to replace all the image tag which contains the occurence of sequence i.e ?custId=1234 with new string cid:
String message = "Need to process image tag <img src=\"http://danny.oz.au/p/56214815-tripod.jpg?custId=1234\"/>";
This what i tried after going thru bit of regex tutorial which replaces all image tag occurence with cid:. I am not getting how to fit the
one more filter i.e ?custId=1234 in regex so that replace only those image tags that contains ?custId=1234
message = message.replaceAll("\\<img.*?>", "cid:");
EDIT:- For example if
input is
"Need to process image tag <img src=\"http://danny.oz.au/p/56214815-tripod.jpg?custId=1234\"/>";
output should be
“Need to process image tag cid:”;
becoz input contains img tag and ?custId=1234 both
input is
"Need to process image tag <img src=\"http://danny.oz.au/p/56214815-tripod.jpg?custId=1235\"/>";
output should be
"Need to process image tag <img src=\"http://danny.oz.au/p/56214815-tripod.jpg?custId=1235\"/>";
becoz input does not contain ?custId=1234 both
Try this: –
For your given input string: –
this will give you: –
Also for input: –
OUTPUT: –
Also, I would suggest you to take a look at
Jsoup - Java HTML Parser, which you should use to parse your HTML. Regex is not a good idea to parse HTML. You can only parse a limited range of tags.You can also use
HTML CleanerUPDATE: –
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: –