I want to replace the number 123 from the below string, but the challenge I am facing is - everytime I replace it, then the number i.e, 1 from name "Xyz1" also got changed. Below is a sample code which I already tried:
import java.util.*;
import java.lang.*;
import java.io.*;
class NumberToString
{
public static void main (String[] args) throws java.lang.Exception
{
String str = "Hello Xyz1, your id is 123";
// str = str.replaceAll("[0-9]","idNew");
// str = str.replaceAll("\\d","idNew");
// str = str.replaceAll("\\d+","idNew");
str = str.replaceAll("(?>-?\\d+(?:[\\./]\\d+)?)","idNew");
System.out.println(str);
}
}
Output of the above code is: Hello XyzidNew, your id is idNew
But, the output which I need is: Hello Xyz1, your id is idNew