我有一个字符串
"This is a big sentence . ! ? ! but I have to remove the space ."
在这句话中,我想删除标点符号之前的所有空格,应该变成
"This is a big sentence.!?! but I have to remove the space."
我正在尝试使用 "\p{Punct}"
但无法替换为字符串。
最佳答案
你应该使用 positive lookahead :
newStr = str.replaceAll("\\s+(?=\\p{Punct})", "")
ideone.com demo for your particular string
表达式分解:
\s
:空白...(?=\\p{Punct})
... 后面是标点符号。
关于java - 在 Java 中删除标点符号前的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6928273/