-1

I have below code snippet:

public static void main(String[] args) {
    String line = "This order was placed by Mr.Dufresne! OK?";
    Pattern pattern = Pattern.compile("(?<=by)(\\s+)(.*?)(\\s+)(?=OK)");
    Matcher matcher = pattern.matcher(line);
    while (matcher.find()) {
        System.out.println("group 0: " + matcher.group(0));
        System.out.println("group 1: " + matcher.group(1));
        System.out.println("group 2: " + matcher.group(2));
        System.out.println("group 3: " + matcher.group(3));
    }
}

The above snippet produces the following output:

group 0:  Mr.Dufresne!
group 1:  
group 2: Mr.Dufresne!
group 3: 

Can someone please explain why it is printing Mr.Dufresne! for all the matcher groups?

And what does this pattern (?<=by) mean?

nhahtdh
  • 52,949
  • 15
  • 113
  • 149
vkreddy
  • 151
  • 5
  • 15

1 Answers1

5

group 0 is the total string matched by regex (See spaces before and after Mr.Dufresne!).

group 2 is the actual group captured.

group 1 is spaces captured.

group 3 is also spaces

(?<=by) is the positive lookbehind.Its a 0 width assertion.It will not consume string.That is why it is not there in group 0.Use by only and it will appear in group 0 as now it will be captured too.

vks
  • 63,206
  • 9
  • 78
  • 110