[英]string.split(“(?!^)”) explanation
I'm trying to split the characters of the String to a String array. 我正在尝试将String的字符拆分为String数组。 I found the solution here .
我在这里找到了解决方案。
The solution is perfect, however I don't get how .split("(?!^)")
worked. 该解决方案是完美的,但是我不了解
.split("(?!^)")
工作方式。 I'm familiar with the basics of split()
method. 我熟悉
split()
方法的基础。 Can someone provide an explanation? 有人可以提供解释吗?
(?!^)
is a regular expression consisting of a negative lookahead . (?!^)
是一个正则表达式,由负前瞻组成。 ^
is an anchor used to signify the start of the string. ^
是用于表示字符串开头的锚点 。 (?!^)
matches all 0-length strings that are not followed by ^
, the start of the string. (?!^)
匹配所有长度为0的字符串,后跟^
(字符串的开头)。 In other words, it matches all 0-length strings except that at the start of the string. 换句话说,它匹配所有0长度的字符串, 但字符串的开头除外 。
For example, in the string abc
, there will be 3 matches: one between a
and b
, one between b
and c
, and one after the c
. 例如,在字符串
abc
,将有3个匹配项:一个在a
和b
之间,一个在b
和c
之间,以及一个在c
。 Splitting on these matches produces the desired array (note that the 1-argument version of split()
discards any trailing empty strings, which is why none are included in the resulting array). 在这些匹配项上进行拆分会生成所需的数组(请注意,
split()
的1参数版本会丢弃所有结尾的空字符串,这就是为什么结果数组中不包含任何空字符串)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.