Even if in a particular case a regex solves your problem with HTML manipulation, regexs are not the proper tools for that work. HTML is not a regular language so you better don't use regex for those tasks. Use an HTML parser. You can achieve your goal pretty easily with Jsoup:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class MyJsoupExample2 {
public static void main(String args[]) {
String inputText = "<html><head></head><body><p><img src=\"test.action&attachmentId=3313&custId=456\" /></p>"
+ "<p>someText <img src=\"getCustomers.do?custCode=2&customerId=3340¶m2=456\"/></p></body></html>";
Document doc = Jsoup.parse(inputText);
Elements myImgs = doc.select("img[src*=attachmentId=3313");
for (Element element : myImgs) {
String src = element.attr("src");
element.attr("src", src.replace("attachmentId=3313", "attachmentId=3824"));
}
System.out.println(doc.toString());
}
}
The code gets the list of img
nodes with a src
attribute containing your target string:
Elements myImgs = doc.select("img[src*=attachmentId=3313");
and loop over the list replacing the value of the src
attribute with your desired value.
I know it is not as appealing as a one-line solution but believe me, it is much better than using a regex. You can find lots of threads on StackOverflow giving the same advice (including this one :).