我试图弄清楚如何组合regex值来做2件事1)删除所有空格2)删除所有标点符号
现在我在用
splitWords = (currentLine.split("\\s+")); 这将删除我正在读取的文件中的所有空白,现在如何添加标点符号?根据API,/p删除所有标点符号。
更新:对不起,我忘记提到我使用split,因为我用空格拆分文件中的一个单词,然后将它们添加到数组中,因此,例如:您在数组中是如何变成: arr = hello,arr1 how,arr 2= are。但是,我不希望数组中的元素有标点符号。
发布于 2015-10-16 06:49:15
您可以在一个字符类中组合多个字符类:
"[\\s\\p{Punct}]+" // Whitespace + Punctuation (POSIX)对于unicode版本,请使用:
"[\\s\\pP]+" // Whitespace + Punctuation (Connector,Dash,Open,Close,InitialQuote,FinalQuote,Other)或者:
"[\\pZ\\pP]+" // Separator (Line,Paragraph,Space) + Punctuation (...)如果您只想删除以下字符,则不需要使用split():
// Returns "HesaidItsaniceworld"
"He said: \"It's a nice world\".".replaceAll("[\\s\\p{Punct}]+", "");发布于 2015-10-16 06:47:05
如果您想替换标点符号和空格,那么为什么要使用split方法?
像这样使用- currentLine.replaceAll("\\s+|[<punctuation marks>]+", "")
其中<punctuation marks>都是标点符号。即[\\!,\\.\\?]
https://stackoverflow.com/questions/33163968
复制相似问题