2

While working on a question to remove duplicate words in a sentence, I found this solution :

(?i)(\b\w+\b)(\s+\1\b)+

So lets say the sentence is

She left LEft leFT now

Now, as far as I understand, the 1st group - "(\b\w+\b)" matches "left" as it should be a boundary character followed by one or more word characters followed by a boundary character again; and 2nd group "(\s+\1\b)" matches "LEft leFT" as it should be (one or more spaces followed by the group 1 match followed by a boundary character) - repeated one or more times.

But when I try to print them like this :

while (matcher.find()) {
    System.out.println (m.group(1) + " - " + m.group(2));
}

The result I expected is

left - LEft leFT

But the actual result is

left - leFT

Can someone explain how this's is happening.

2012178
  • 83
  • 3

0 Answers0