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?