How to replace last dot in a string using a regular expression?

后端 未结 4 1151
小蘑菇
小蘑菇 2021-01-05 09:25

I\'m trying to replace the last dot in a String using a regular expression.

Let\'s say I have the following String:

String string = \"hello.world.how         


        
4条回答
  •  情话喂你
    2021-01-05 10:11

    One way would be:

    string = string.replaceAll("^(.*)\\.(.*)$","$1!$2");
    

    Alternatively you can use negative lookahead as:

    string = string.replaceAll("\\.(?!.*\\.)","!");
    

    Regex in Action

提交回复
热议问题