Java Regex To Uppercase

后端 未结 4 892
萌比男神i
萌比男神i 2021-01-19 10:27

So I have a string like

Refurbished Engine for 2000cc Vehicles

I would like to turn this into

Refurbish

4条回答
  •  旧巷少年郎
    2021-01-19 11:26

    You can try with

    text = text.replaceAll("(\\d{4})cc", "$1CC");
    //                          ↓          ↑
    //                          +→→→→→→→→→→+
    

    Trick is to place number in group (via parenthesis) and later use match from this group in replacement part (via $x where x is group number).

    You can surround that regex with word boundaries "\\b" if you want to make sure that matched text is not part of some other word. You can also use look-adound mechanisms to ensure that there are no alphanumeric characters before and/or after matched text.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题