2

Apparently the split function in Java has changed from Java 7 to Java 8. ( More here: Why in Java 8 split sometimes removes empty strings at start of result array? )

Some people are suggesting to use split("?!^") instead of split("") (In Java 7).

My main question is how to interpret /(?!^)/? Is there any case where it is different from //?

Community
  • 1
  • 1
Daniel
  • 5,839
  • 9
  • 46
  • 85

1 Answers1

3

First of all, the suggested regex is split("(?!^)") (as opposed to the invalid regex you posted). (?!^) is a negative lookahead that matches anywhere except at ^ (the beggining of the string).

As you already mentioned, the behaviour of split() changed in Java 8, and a zero-width match at the beginning however never produces such empty leading substring.

Therefore, if you use split("(?!^)") you will get the same behaviour independent of the Java version.

Mariano
  • 6,423
  • 4
  • 31
  • 47