在Java中标点符号之前删除空格

Arj*_*jit 4 java regex string

我有一个字符串

"This is a big sentence .  !  ?  !  but I have to remove the space ."   
Run Code Online (Sandbox Code Playgroud)

在这句话中,我想删除标点符号之前的所有空格,并且应该成为

"This is a big sentence.!?!  but I have to remove the space."   
Run Code Online (Sandbox Code Playgroud)

我试图使用"\p{Punct}"但不能替换字符串.

aio*_*obe 10

你应该使用积极的前瞻:

newStr = str.replaceAll("\\s+(?=\\p{Punct})", "")
Run Code Online (Sandbox Code Playgroud)

ideone.com演示您的特定字符串

细分表达式:

  • \s:白色空间......
  • (?=\\p{Punct}) ......之后是标点符号.