问题描述
我想沿着非字母字符将String拆分为String数组。例如:
I want to split a String into a String array along non-alphabetic characters. For example:
"Here is an ex@mple" => "Here", "is", "an" "ex", "mple"
我试过用方法,带正则表达式(?![\\\\ {{alpha}])
。然而,这将字符串拆分为
I tried using the String.split(String regex) method with the regular expression "(?![\\p{Alpha}])"
. However this splits the string into
"Here", "_is", "_an", "_ex", "@ample"
(这些下划线强调有空格)。我想这是因为?!
正则表达式运算符是零宽度并且实际上是拆分并删除输入中非字母字符前面的零宽度字符string。
(those underscores are to emphasize there is a space). I guess this is because the ?!
regex operator is "zero-width" and is actually splitting on and removing a zero-width character preceding the non-alphabetic characters in the input string.
在分割字符串时,如何完成删除实际的非字母字符?是否存在非零宽度否定运算符?
How can I accomplish removal of the actual non-alpha characters while I split the string? Is there a NON-zero-width negation operator?
推荐答案
您可以尝试 \ P {Alpha} +
:
You could try \P{Alpha}+
:
"Here is an ex@mple".split("\\P{Alpha}+")
["Here", "is", "an", "ex", "mple"]
\\ \\ P {Alpha}
匹配任何非字母字符(与 \p {Alpha}
相对,它匹配任何字母字符)。 +
表示我们应该拆分任何连续的此类字符串。例如:
\P{Alpha}
matches any non-alphabetic character (as opposed to \p{Alpha}
, which matches any alphabetic character). +
indicates that we should split on any continuous string of such characters. For example:
"a!@#$%^&*b".split("\\P{Alpha}+")
["a", "b"]
这篇关于非字母字符上的Java字符串拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!