how to replace multiple matched Regex

后端 未结 2 1887
感情败类
感情败类 2021-01-03 07:11

I have a set of regex replacements that are needed to be applied to a set of String,

For example:

  1. all multiple spaces with single space (\"\\s{
2条回答
  •  孤独总比滥情好
    2021-01-03 07:53

    You have these 2 replaceAll calls:

    s = s.replaceAll("\\s{2,}"," ");
    s = s.replaceAll("\\.([a-zA-Z])",". $1");
    

    You can combine them into a single replaceAll like this:

    s = s.replaceAll("\\s{2,}|(\\.)(?=[a-zA-Z])", "$1 ");
    

    RegEx Demo

提交回复
热议问题