javaregexreluctant-quantifiers

Java reluctant matching from both sides of the quantifier


I would like to write a regex in java which will find the shortest available match from both sides of the quantifier.

For example, for this pattern "a.*?b" apllied to "aaaaaacbbbbbbb":

i would like to find "acb" but i get "aaaaaacb".

Anyone has an idea which regex i can use for that?


Solution

  • You could mimic reluctant matching from the start by discarding a greedy .*. In this case, this would become .*(a.*?b), if you get group number 1:

    String example = "aaaaaacbbbbbbb";
    Matcher m = Pattern.compile(".*(a.*?b)").matcher(example);
    m.find();
    System.out.println(m.group(1));