\G
in Java was added in Java 6 to mimic the Perl construct:
http://perldoc.perl.org/perlfaq6.html#What-good-is-%5CG-in-a-regular-expression:
You use the \G anchor to start the next match on the same string where the last match left off.
Support for this was very poor. This construct is documented in Python to be used in negative variable-length lookbehinds to limit how far back the lookbehind goes. Explicit support was added.
However, the split method in JDK 7 has a fast path for the common case where the limit is a single character. This avoids the need to compile or to use regex. Here is the method (detail source redacted):
public String[] split(String regex, int limit) {
/* fastpath if the regex is a
(1)one-char String and this character is not one of the
RegEx's meta characters ".$|()[{^?*+\\", or
(2)two-char String and the first char is the backslash and
the second is not the ascii digit or ascii letter.
*/
char ch = 0;
if (((regex.value.length == 1 &&
".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
(regex.length() == 2 &&
/* Fast path checks as documented in the comment */ )
{
// Fast path computation redacted!
String[] result = new String[resultSize];
return list.subList(0, resultSize).toArray(result);
}
return Pattern.compile(regex).split(this, limit);
}
And before:
public String[] split(String regex, int limit) {
return Pattern.compile(regex).split(this, limit);
}
While this fastpath exists, note that deploying Android programs means it must have source compatibility with Java 6. The Android environment is unable to take advantage of the fast path, therefore it delegates to fastSplit
and loses some of the Perl construct supports, such as \A
.
As for why they didn't like the traditional always-regex path, it's kind of obvious by itself.