jeudi 28 mars 2019

Replace A Pattern In String Using Java

I need to replace test word up to next comma in a string.
For example the string is "abc test xy, tmy test 1, vks , csb";
The output after replacement should be "abc , tmy, vks,csb".
Removed test followed by any character or number up to comma.

I tried the following code but did not work.

import java.sql.Timestamp;
import java.time.Instant;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class TESTREPLACE {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //String str = " DEFAULT REPLACEME NOT NULL,";
        String str = "abc test   xy, tcs test  1, vks , csb ";

        String regex = " default.+\\,";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        System.out.println(matcher.replaceAll("\\,"));
    }
}

Aucun commentaire:

Enregistrer un commentaire