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? )
显然,Java中的split函数已经从Java 7更改为Java 8。(更详细地说:为什么在Java 8拆分中,有时会在结果数组开始时删除空字符串?)
Some people are suggesting to use split("?!^")
instead of split("")
(In Java 7).
一些人建议使用分裂(“? ! ^”)而不是分裂(" ")(在Java 7)。
My main question is how to interpret /(?!^)/
? Is there any case where it is different from //
?
我的主要问题是如何解释/(? ! ^)/ ?是否有不同于//的情况?
1 个解决方案
#1
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).
首先,建议regex分裂((? ! ^))(而不是您张贴的无效的正则表达式)。(? ! ^)是一个负面超前相匹配除了在^(字符串)的发出召唤。
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.
正如您已经提到的,split()的行为在Java 8中发生了变化,并且在开始时进行了零宽度匹配,但是从来不会产生这样的空前导子字符串。
Therefore, if you use split("(?!^)")
you will get the same behaviour independent of the Java version.
因此,如果您使用分裂((? ! ^))你会得到同样的行为独立于Java版本。
#1
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).
首先,建议regex分裂((? ! ^))(而不是您张贴的无效的正则表达式)。(? ! ^)是一个负面超前相匹配除了在^(字符串)的发出召唤。
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.
正如您已经提到的,split()的行为在Java 8中发生了变化,并且在开始时进行了零宽度匹配,但是从来不会产生这样的空前导子字符串。
Therefore, if you use split("(?!^)")
you will get the same behaviour independent of the Java version.
因此,如果您使用分裂((? ! ^))你会得到同样的行为独立于Java版本。