0

I couldn't find where I was wrong for the following java code.

My goal:

I want to replace the value between START_ and _END tags with a fixed value....

Input:

START_1111111sdf111_END,START_2222dsdg f22222222_END,START_0000000000_END my test...

Output:

START_1111111sdf111_END,START_2222dsdg f22222222_END,START_0000000000_END my test...

Expected Output:

START_xxxxxxxxxxx_END,START_xxxxxxxxxxx_END,START_xxxxxxxxxxx_END my test...

Thanks...

private static void replaceFiledVlue(String text) {
    String start="START_";
    String end="_END";
    String value= start+"1111111sdf111"+end+ ","+start+"2222dsdg f22222222"+end+","+start+"0000000000"+end+" my test...";
    String replacement=start+"xxxxxxxxxxx"+end;
    value=value.replaceAll("("+start+"(.(?!"+start+"|"+end+"))*"+end+")" , replacement);

    System.out.print(value);
    //RESULT: START_1111111sdf111_END,START_2222dsdgf 22222222_END,START_0000000000_END my test...
    //EXPECTED:START_xxxxxxxxxxx_END,START_xxxxxxxxxxx_END,START_xxxxxxxxxxx_END my test...
}
bmck
  • 211
  • 2
  • 6

3 Answers3

4

You can use non-greedy regular expressions to simplify your task:

var pattern = Pattern.compile("START_(?:.+?)_END");
pattern.matcher(input).replaceAll("START_xxxxx_END");

Since your output is fixed, you do not even need any capturing groups. Simply match everything from START till END and then replace it with the expected output (effectively replacing START with START, the inner content with XXXX and END with END).

knittl
  • 246,190
  • 53
  • 318
  • 364
  • The solution that @sagi directed me is also correct, but this solution is the best. Thank you so much. – bmck May 27 '20 at 18:06
0

Please check below:

public class Main
{
    public static void main(String[] args) {
    String start="START_";
    String end="_END";
    String value= start+"1111111sdf111"+end+ ","+start+"2222dsdg f22222222"+end+","+start+"0000000000"+end+" my test...";
    String replacement=start+"xxxxxxxxxxx"+end;
    value=value.replaceAll("("+start+")[^&,]*("+end+")" , replacement);

    System.out.print(value);
    //RESULT: START_1111111sdf111_END,START_2222dsdgf 22222222_END,START_0000000000_END my test...
    //EXPECTED:START_xxxxxxxxxxx_END,START_xxxxxxxxxxx_END,START_xxxxxxxxxxx_END my test...

    }
}

enter image description here

Harmandeep Singh Kalsi
  • 3,315
  • 2
  • 14
  • 26
  • `[^&,]*` part of regex is not required here. Although your answer is working but regex you used makes different sense i.e. Match everything except `&` and `,`. For example; your regex will fail this string `START_bskskf&vnsvnsnnsnsc_END`. Notice; `&` sign. What you can do it simply put `.*?` –  May 27 '20 at 18:00
  • 1
    My bad , I missed that part . Thank you for pointing it out. I updated the code and screenshot accordingly. – Harmandeep Singh Kalsi May 27 '20 at 18:09
  • Sure , definitely. I will keep that in mind. I might not have posted the image . But I had to do that since there was some comment saying I copied from somewhere and all . – Harmandeep Singh Kalsi May 27 '20 at 18:16
0
private static void replaceFiledVlue(String text) {
        String start="START_";
        String end="_END";
        String value= start+"1111111sdf111"+end+ ","+start+"2222dsdg f22222222"+end+","+start+"0000000000"+end+" my test...";
        String replacement=start+"xxxxxxxxxxx"+end;
        value=value.replaceAll(String.format("%s(.*?)%s", start, end) , replacement);

        System.out.print(value);
        //RESULT: START_1111111sdf111_END,START_2222dsdgf 22222222_END,START_0000000000_END my test...
        //EXPECTED:START_xxxxxxxxxxx_END,START_xxxxxxxxxxx_END,START_xxxxxxxxxxx_END my test...
    }
s_bighead
  • 1,014
  • 13
  • 24