1

I want to replace only exactly matching link given in String. My code is as follows:

String originalString = "<a target=\"_blank\" href=\"http://example.com/\"><span style=\"font-size: 12px;\">ABC</span></a>"
            + "<a target=\"_blank\" href=\"http://example.com/contact/\"><span style=\"font-size: 12px;\">Contact</span></a>";

String replacedString = originalString.replace("http://example.com/", "link1");
System.out.println("Replaced String:" + replacedString);


replacedString = "<a target="_blank" href="link1"><span style="font-size: 12px;">ABC</span></a><a target="_blank" href="link1contact/"><span style="font-size: 12px;">Contact</span></a>"

requiredString = "<a target="_blank" href="link1"><span style="font-size: 12px;">ABC</span></a><a target="_blank" href="link2"><span style="font-size: 12px;">Contact</span></a>"

I get Output as replacedString but required Output should be as requiredString.

Thanks in advance.

3 Answers3

1

Replace the URL with the quotes:

String replacedString = originalString.replace("\"http://example.com/\"", "\"link1\"");
replacedString = replacedString.replace("\"http://example.com/contact/\"", "\"link2\"");
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
0

The problem is that http://example.com/contact/ contains http://example.com/. Use this instead:

String replacedString = originalString.replace("http://example.com/contact/", "link2");
String replacedString2 = replacedString.replace("http://example.com/", "link1");

replacedString2 is the required output

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
  • replaceAll() also gives the same answer, it replaces both link. But I want "http://example.com/contact/" to be replaced with link2 – Twinkle Soni Dec 01 '16 at 12:52
  • @TwinkleSoni Updated my answer, now it does it right – ItamarG3 Dec 01 '16 at 12:55
  • replaceAll takes a regexp as the first argument. replace is the way to go – Maurice Perry Dec 01 '16 at 12:57
  • @MauricePerry right. I accidentally put `replaceAll()` – ItamarG3 Dec 01 '16 at 12:58
  • Then, I would suggest to create a map somewhere. because this will end up with multiple replace call for link3, link4, link5, ... ;) – AxelH Dec 01 '16 at 13:00
  • @AxelH there is no `link3,4,5` – ItamarG3 Dec 01 '16 at 13:01
  • 1
    There was no `link2` in his question (except in the needed result). Something tells me this will be a problem pretty soon ;) – AxelH Dec 01 '16 at 13:03
  • @AxelH - that is true... I'm working on an implementation – ItamarG3 Dec 01 '16 at 13:07
  • @ItamarGreen it is working, but the order of links is not fixed. In my actual code I am extracting Links one by one and replacing them dynamically. So if example.com will come first then it will replace all strings. – Twinkle Soni Dec 01 '16 at 13:09
  • @TwinkleSoni could you please give an example of a longer url? just to make it clearer how it should be – ItamarG3 Dec 01 '16 at 13:10
  • @ItamarGreen don't bother OP doesn't need to add requirements ! He asked to replace a String. Nothing more. – AxelH Dec 01 '16 at 13:12
  • @AxelH - I don't think that's very nice to do that. OP needs help and I'm trying to give it (But if you think the answer is enough, by all means) – ItamarG3 Dec 01 '16 at 13:13
  • @TwinkleSoni as suggested earlier check here and implement one for yours. http://stackoverflow.com/questions/6745154/how-to-replace-multiple-words-in-a-single-string-in-java – Imran Dec 01 '16 at 13:14
  • 1
    @ItamarGreen, well OP doesn't care about your solution apparently ;) just want working codes... – AxelH Dec 01 '16 at 13:16
-3

working regex ishttp:\\/\\/example.com.*?(?=\\\\) in java. it matches all occurences of http://example.com and until the next backslash

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65