Java regex lookahead non-capture but output it

I am trying to use the \ w pattern (? = \ W) to find 2 consecutive characters using the following: although lookahead works, I want to output the actual match, but not consume it

here is the code:

Pattern pattern = Pattern.compile("\\w(?=\\w)");
Matcher matcher = pattern.matcher("abcde");

while (matcher.find())
{
    System.out.println(matcher.group(0));
}

I need the appropriate output: ab bc cd de

but i can only get abcde

any idea?

+4
source share
1 answer

The lookahead content is zero width, so it is not part of the zero group. To do what you want, you need to explicitly capture the contents of lookahead and then restore the combined text + lookahead, for example:

Pattern pattern = Pattern.compile("\\w(?=(\\w))");
//                                       ^   ^
//                                       |   |
//                             Add a capturing group

Matcher matcher = pattern.matcher("abcde");

while (matcher.find()) {
    // Use the captured content of the lookahead below:
    System.out.println(matcher.group(0) + matcher.group(1));
}

Demo on ideon.

+4

Source: https://habr.com/ru/post/1543182/


All Articles