Home > Mobile >  get substring from string upto any special chars occurs
get substring from string upto any special chars occurs

Time:10-11

In java get a substring from string up to any special chars occurs the below coding is working as expected but in the future, some other special chars exist that need the additional if condition, can we have any regex to get a substring

private static String getTruncatedVesselName(String input){
        if(input.contains("/")){
            String output = input.split("/")[0];
            return getSubString(output);
        }
        return getSubString(input);
    }
    private static String getSubString(String output){
        if(output.contains("(")){
            return output.substring(0,output.indexOf("("));
        }
        return output;
    }
Ex: 
    INPUT                 Expected OUTPUT
   abc def a(xy) / pq           abc def a
   abc def a(xy)                abc def a
   abc def a / pq               abc def a
   abc def a/pq (xy)            abc def a

CodePudding user response:

I assume, if you want a substring until any special character occur in input string, then here is solution below which based on regex.

String inputs[] = {
    "abc def a(xy) / pq",
    "abc def a(xy)",
    "abc def a / pq",
    "abc def a/pq (xy)"
};

for(String input : inputs) {
    System.out.println(input   "\t"   getSubString(input));
}

private static String getSubString(String input) {
    Matcher matcher = Pattern.compile("[\\w\\s]*").matcher(input);
    if(matcher.find()) {
        return input.substring(0, matcher.end());
    }
    return input;
}

And here is your desired result

abc def a(xy) / pq      abc def a
abc def a(xy)           abc def a
abc def a / pq          abc def a 
abc def a/pq (xy)       abc def a

CodePudding user response:

If you want to match first word or whitespace chars you can use

Pattern.compile("^[\\w\\s] ");

Or, if you need to support any Unicode words:

Pattern pattern = Pattern.compile("(?U)^[\\w\\s] ");

Here, ^[\w\s] matches one or more ( ) chars that are either letters/digits/underscore (\w) or whitespace (\s) at the start of the string (^).

See the regex demo.

If you want to prevent trailing whitespace, you can use

Pattern pattern = Pattern.compile("^\\w (?:\\s \\w )*");
Pattern pattern = Pattern.compile("(?U)^\\w (?:\\s \\w )*");

See the Java code:

String s = "abc def a(xy) / pq";
Pattern pattern = Pattern.compile("^\\w (?:\\s \\w )*");
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
    System.out.println(matcher.group(0)); 
} 

Output:

abc def a

See the regex demo. Details:

  • ^ - start of a string
  • \w - one or more word chars
  • (?:\s \w )* - zero or more sequences of
    • \s - one or more whitespaces
    • \w - one or more word chars
  • Related