Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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) {
    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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading