java字符串拆分所有非字母数字除撇号 [英] java string split on all non-alphanumeric except apostrophes
本文介绍了java字符串拆分所有非字母数字除撇号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我想在任何非字母数字字符上拆分java中的字符串。
So I want to split a string in java on any non-alphanumeric characters.
目前我一直在这样做
words= Str.split("\\W+");
但是我想在那里保留撇号(')。是否有任何正则表达式来保留撇号但踢掉其余的垃圾?谢谢。
However I want to keep apostrophes("'") in there. Is there any regular expression to preserve apostrophes but kick the rest of the junk? Thanks.
推荐答案
words = Str.split("[^\\w']+");
只需将其添加到角色类。 \ W
相当于 [^ \w]
,然后您可以添加 '
to。
Just add it to the character class. \W
is equivalent to [^\w]
, which you can then add '
to.
请注意, \w
实际上也包括下划线。如果你想在下划线上拆分,你应该使用 [^ a-zA-Z0-9']
代替。
Do note, however, that \w
also actually includes underscores. If you want to split on underscores as well, you should be using [^a-zA-Z0-9']
instead.
这篇关于java字符串拆分所有非字母数字除撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文