Search code examples
javaregexregex-groupregex-greedyboost-regex

How get numeric between two character with regex java?


I have the string as follows :

SUB8&20.000,-&succes&09/12/18SUB12&100.000,-&failed&07/12/18SUB16&40.000,-&succes&09/12/18

I want to get a string "8&20.000","16&40.000" between SUB and ,-&succes


I want to get succes data how to get the string using java regex ?


Solution

  • Use this regex,

    SUB([^,]*),-&succes
    

    Java code,

    public static void main(String[] args) {
        String s = "SUB8&20.000,-&succes&09/12/18SUB12&100.000,-&failed&07/12/18SUB16&40.000,-&succes&09/12/18";
        Pattern p = Pattern.compile("SUB([^,]*),-&succes");
        Matcher m = p.matcher(s);
        while (m.find()) {
            System.out.println(m.group(1));
        }
    }
    

    Prints,

    8&20.000
    16&40.000
    

    Check here