6

Same regex, different results;

Java

String regex = "Windows(?=95|98|NT|2000)";
String str = "Windows2000";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
System.out.println(m.matches()); // print false

JavaScript

var value = "Windows2000";
var reg = /Windows(?=95|98|NT|2000)/;
console.info(reg.test(value)); // print true

I can't understand why this is the case?

adiga
  • 34,372
  • 9
  • 61
  • 83
yuge
  • 71
  • 3
  • 2
    It could be because `matches()` returns true when the **whole** string to test matches. (the positive lookahead isn't part of the match) – Cid Dec 27 '19 at 08:40

1 Answers1

5

From the documentation for Java's Matcher#matches() method:

Attempts to match the entire region against the pattern.

The matcher API is trying to apply your pattern against the entire input. This fails, because the RHS portion is a zero width positive lookahead. So, it can match Windows, but the 2000 portion is not matched.

A better version of your Java code, to show that it isn't really "broken," would be this:

String regex = "Windows(?=95|98|NT|2000)";
String str = "Windows2000";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
while (m.find()) {
    System.out.println(m.group()); // prints "Windows"
}

Now we see Windows being printed, which is the actual content which was matched.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 1
    @VishwaRatna Lookarounds are **zero width**, meaning _nothing_ will ever be captured/consumed from a lookahead (OK...there are exceptions, such as a backreference which was captured in a lookaround, but that's an edge case). Just run my Java code and you will see what is happening here. – Tim Biegeleisen Dec 27 '19 at 08:44
  • @TimBiegeleisen String#matcher() also returns false; – yuge Dec 27 '19 at 09:05