gpt4 book ai didi

java - 在 Java 中删除标点符号前的空格

转载 作者:搜寻专家 更新时间:2023-11-01 01:23:28 27 4
gpt4 key购买 nike

我有一个字符串

"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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com