Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same regex have different results in Java and JavaScript [duplicate]

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?

like image 873
yuge Avatar asked Dec 27 '19 08:12

yuge


1 Answers

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.

like image 112
Tim Biegeleisen Avatar answered Oct 19 '22 15:10

Tim Biegeleisen