Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should capturing parentheses affect a separate negative lookahead?

Tags:

java

regex

I am using Java. I have the following text:

"hyst and hy"

Why (hy)(?![a-z]) returns two "hy"s. The idea is to match any "hy" that is not followed by any character between a-z.

If I do hy(?![a-z]) (hy without parentheses) it works (finds only the second "hy") but I don't understand why if I use parentheses (hy) in the RegEx it matches the first "hy" in hyst

like image 820
user2287359 Avatar asked Apr 30 '13 22:04

user2287359


1 Answers

When you use a capture group you obtain two results, the first is the whole pattern and the second the capture group. The first hy has never been matched.

If you remove the parenthesis, you obtain only that match the whole pattern.

like image 110
Casimir et Hippolyte Avatar answered Oct 24 '22 14:10

Casimir et Hippolyte