0

i have customerText as below

 String customerText="TestBody <img src="test.action&attachmentId=3313&custId=456 /> Sometext @attachmentId=3313";

i want to replace all occurence of attachmentId=3313 (lying with in image tag) with attachmentId=3824.

So my expected output for above input is

output is

 "TestBody <img src="test.action&attachmentId=3824&custId=456 /> Sometext @attachmentId=3313";
M Sach
  • 33,416
  • 76
  • 221
  • 314

3 Answers3

1

use this regex (?<=test\.action&attachmentId=)(\d+)(?=&|$|(/>)| ) to replace with 3824

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
1

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&param2=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 :).

Community
  • 1
  • 1
Vicent
  • 5,322
  • 2
  • 28
  • 36
0

If you're not dead set on using regex, the easiest solution would be a simple String replace:

String customerText="TestBody <img src=\"test.action&attachmentId=3313&custId=456\" />";
String output = customerText.replace("attachmentId=3313", "attachmentId=3824");
JGaarsdal
  • 241
  • 1
  • 6