Search code examples
javastringdelimiter

Parse from end to start of a String to grab data before the third occurrence of a delimiter


I am working on some strings and trying to parse through the data and retrieve a string that lies before the third occurrence of " - " from the end of the string. This data comes as a String from the DB and there is some text "-NONE----" that I would like to exclude while parsing.

Input (Below input is a String and not List)

String input1 = "-A123456-B987-013691-000-109264821"
String input2 = "-NONE----"
String input3 = "C1234567-A1241-EF-012361-000-18273460"

Output

String output1 = "-A123456-B987"
String output2= "-NONE----"
String output3 = "C1234567-A1241-EF"

Starting from the beginning of my string, I need to retrieve data before the third occurrence of
" - " (hyphen) is found, but I need to count " - " (hyphen) occurrence starting from end of string.

Any tips are appreciated.


Solution

  • You could match the three dashes from behind with the $ symbol and then extract everything that is in front of that. I created two capture groups, where the first one is what you want to extract:

    private static String extractFront(String input1) {
        if(input1.equals("-NONE----")) {
           return input1;
        } else {
        Pattern pattern = Pattern.compile("(.*)(-[^-]*){3}$");
        Matcher matcher = pattern.matcher(input1);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
        }
    }
    

    Main to test:

    public static void main(String[] args) {
        String input1 = "-A123456-B987-013691-000-109264821";
        String input2 = "-NONE----";
        String input3 = "C1234567-A1241-EF-012361-000-18273460";
    
        System.out.println(extractFront(input1));
        System.out.println(extractFront(input2));
        System.out.println(extractFront(input3));
    }
    

    Output:

    -A123456-B987
    -NONE----
    C1234567-A1241-EF
    

    EDIT: @stubbleweb1995 added the if condition for a complete solution