Home > Net >  Exclude/remove a string between two special characters using regex
Exclude/remove a string between two special characters using regex

Time:10-08

I was trying to replace/remove any string between - <branch prefix> /

Example:

String name = Application-2.0.2-bug/TEST-1.0.0.zip

expected output :

Application-2.0.2-TEST-1.0.0.zip

I tried the below regex, but it's not working accurate.

String FILENAME = 2.2.1-Application-2.0.2-bug/TEST-1.0.0.zip

println(FILENAME.replaceAll(". /", ""))

CodePudding user response:

There can be many ways e.g. you can replace \w \/ with a "". Note that \w means one or more word characters.

Demo:

public class Main {
    public static void main(String[] args) {
        String FILENAME = "Application-2.0.2-bug/TEST-1.0.0.zip";
        FILENAME = FILENAME.replaceAll("\\w \\/", "");
        System.out.println(FILENAME);
    }
}

Output:

Application-2.0.2-TEST-1.0.0.zip

ONLINE DEMO

  • Related