string.split("(?!^)")解释 [英] string.split("(?!^)") explanation
问题描述
我正在尝试将String的字符拆分为String数组。我找到了解决方案此处。
I'm trying to split the characters of the String to a String array. I found the solution here.
解决方案是完美的,但我不知道如何 .split((?!^))
工作。我熟悉 split()
方法的基础知识。有人可以提供解释吗?
The solution is perfect, however I don't get how .split("(?!^)")
worked. I'm familiar with the basics of split()
method. Can someone provide an explanation?
推荐答案
(?!^)
是正则表达式,由否定前瞻。 ^
是锚点表示字符串的开头。 (?!^)
匹配所有0长度的字符串,后面跟着 ^
,字符串的开头。换句话说,它匹配字符串开头的除之外的所有0长度字符串。
(?!^)
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. In other words, it matches all 0-length strings except that at the start of the string.
例如,在字符串<$中c $ c> abc ,将有3个匹配:一个在 a
和 b
之间,一个在 b
和 c
之间,一个在 c
。拆分这些匹配会产生所需的数组(请注意, split()
的1参数版本会丢弃任何尾随的空字符串,这就是为什么没有包含在结果数组中的原因) 。
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
. 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).
这篇关于string.split("(?!^)")解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!