0

Replace a pattern in a long String. Used replace method in a loop, but every time it will replace the first occurrence.

I want to Iterate and replace all occurrences one by one for example

LongString.contains("/ima?pth=") 
LongString=LongString.replace("/ima?pth=", "/ima?pth=**1**");

Every time I have to add a new number at the end of the occurrence it can be any value. How to do this in Java?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Jimmy_R
  • 21
  • 4

4 Answers4

0

According to String.replaceAll without RegEx it should replace all... Otherwise you could put in a while loop. I caution this though, as LongString.contains("/ima?pth=") would still be true when it is all replaced. Would be easy to have an infinite loop.

Community
  • 1
  • 1
0

Use Matcher and Matcher#appendReplacement()

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReplaceAllEx {

    public static void main(String[] args) {
        String input = "randomPrefix"
                + "/ima?pth=randomInfix/ima?pth="
                + "/ima?pth=randomInfix/ima?pth="
                ;
        Pattern p = Pattern.compile("/ima\\?pth=");
        Matcher m = p.matcher(input);
        int i = 1;
        StringBuffer sb = new StringBuffer();
        System.out.println("input is\n" + input + "\n");
        while (m.find()) {
            m.appendReplacement(sb, "/ima?pth="+"**"+i+"**");
            i++;
        }
        System.out.println("output is\n" + sb.toString() + "\n");
    }

}

The output of the above program would be:

input is
randomPrefix/ima?pth=randomInfix/ima?pth=/ima?pth=randomInfix/ima?pth=

output is
randomPrefix/ima?pth=**1**randomInfix/ima?pth=**2**/ima?pth=**3**randomInfix/ima?pth=**4**

Updated

Here is another example demonstrating the use of Matcher#appendReplacement() -- with OP's new input example.

private static void example2() {
    String input = "<img src=\"cid:1\"/>"
    private static void example2() {
        String input = "<img src=\"cid:1\"/>"
//              + "<span style=\"font-size:14pt;font-family:'Times New Roman';\">"
//              + "</span><div>"
//              + "<span style=\"font-size:14pt;font-family:'Times New Roman';\">"
//              + "<br /></span></div>"
//              + "<div>"
//              + "<span style=\"font-size:14pt;font-family:'Times New Roman';\">"
//              + "<br /></span></div>"
//              + "<div>"
//              + "<span style=\"font-size:14pt;font-family:'Times New Roman';\">"
//              + "</span><br /><div style=\"display:none;\">"
//              + "</div></div><div>"
//              + "<span style=\"font-size:14pt;font-family:'Times New Roman';\">"
//              + "<br /></span></div><div>"
                + "<img src=\"/XP/image?path=09021b5e80061f2c\"/>"
//              + "<span style=\"font-size:14pt;font-family:'Times New Roman';\">"
//              + "<br /></span>"
                + "<img src=\"/XP/image?path=09021b5e80061f2c\"/>"
                + "<img src=\"/XP/image?path=09021b5e80061f2c\"/>"
                ;
        Pattern p = Pattern.compile("<img src=\\\"/XP/image\\?path=09021b5e80061f2c\\\"/>");
        Matcher m = p.matcher(input);
        int i = 2;
        StringBuffer sb = new StringBuffer();
        System.out.println("input is\n" + input + "\n");
        while (m.find()) {
            String replacement = String.format("<img src=\"cid: %d\"/>", i);
            m.appendReplacement(sb, replacement);
            i++;
        }
        System.out.println("output is\n" + sb.toString() + "\n");

    }

This would be the program output:

input is
<img src="cid:1"/><img src="/XP/image?path=09021b5e80061f2c"/><img src="/XP/image?path=09021b5e80061f2c"/><img src="/XP/image?path=09021b5e80061f2c"/>

output is
<img src="cid:1"/><img src="cid: 2"/><img src="cid: 3"/><img src="cid: 4"/>
leeyuiwah
  • 6,562
  • 8
  • 41
  • 71
0

You could use a StringBuilder like so:

class Main {
  public static void main(String[] args) {
    String longString = "qwe/ima?pth=asdzxc/ima?pth=qweasd/ima?pth=sdf";
    StringBuilder str = new StringBuilder(longString);
    String pattern = "/ima?pth=";
    int index = longString.indexOf(pattern);
    int number = 1;
    while(index >= 0) {
     str.insert(index + pattern.length(), number);
     index = str.indexOf("/ima?pth=", index+1);
     number += 1;
    }
    longString = str.toString();
    System.out.println(longString); //qwe/ima?pth=1asdzxc/ima?pth=2qweasd/ima?pth=3sdf
  }
}

You could modify the operations on number as desired but in this instance it only goes up by 1.

Try it here!

Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
0

replaceAll(String regex, String replacement)

Should have you covered

Ben Arnao
  • 492
  • 5
  • 11