问题描述
我试图将字符串拆分为字符数组字符串,问题是 .split()
也返回一个空元素。 (test)。split
将返回 [,t,e,s,t]
。
I was trying to split a string into an array of characters string, the problem is that .split()
returns an empty element also. ("test").split
would return ["","t","e","s","t"]
.
此问题中的解决方案解决了问题(使用 .split((?!^))
)。
The solution in this question Split string into array of character strings solves the problem ( using .split("(?!^)")
).
但是我仍然无法理解为什么会这样,我不会使用一段我无法理解的代码只是因为它得到了工作完成。
However I still cannot understand why this works, and i'm not going to use a piece of code which i cannot understand just because it gets the job done.
我读过这两页和关于负面预测仍然无法理解。有人可以澄清这个吗?
I've read these two pages http://www.regular-expressions.info/lookaround.html and http://ocpsoft.org/opensource/guide-to-regular-expressions-in-java-part-2/ about negative look-ahead and still cannot understand. Can someone clarify this?
推荐答案
使用(test)。split()
会将字符串拆分到字符前的每个位置,从而产生 [,t,e,s,t]
,因为第一次拆分(在 t
之前)将导致空条目。
using ("test").split()
will split the string at EVERY position before a character, resulting in ["", "t", "e", "s", "t"]
, because the first split (in front of t
) will cause an empty entry.
此正则表达式((?!^)
)的意思是:在每个字符处拆分字符串,其中不是行开头(^)是前一个字符 *:
This regex ("(?!^)"
) does mean: Split the string at every Character, where NOT the line-start (^) is the previous character*:
你的字符串基本上看起来(对于Regex引擎)是这样的: ^ test $
所以,正则表达式将执行每个拆分,除了在第一个 t
之前的拆分,因为它匹配 ^
- 并且它当前位置前面的字符是 ^
(字符串/行开始)时,不应拆分。
Your string basically looks (for the Regex Engine) like this: ^test$
So, the regex will perform EVERY split, except the split before the first t
, because there it matches the ^
- and it should NOT split, when the char in front of the current position is the ^
(String / Line-Start).
* actualley ^
不是一个字符,这就是为什么你在 $
之前没有另一个分割 - 它们是只是元字符 - 所以t o说。
*actualley the ^
is not a character, thats why you dont have another split before the $
- they are just meta-characters - so to say.
这篇关于Java使用负前瞻将字符串拆分为字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!