简体   繁体   中英

Correct the word "urgent" with repeated letters using regular expression in Java

I want to change "Hitttt Urrggeennnttt Rrare" with "Hitttt Urgent Rrare" using regex, or "Uupppp Uuurrggeent Upp!! Barrkk" with "Uupppp Urgent Upp!! Barrkk". (All words are in camel case.)

I mean I just want to correct the 'urgent's, not the other words.

s.replaceAll("([Urgent])\\1+", "$1");

does not work. Any idea?

This should work for you:

public static void main(String[] args) {
    String s = "hitttt Urrggeennnttt rrare";
    s = s.replaceAll("(?i)u+r+g+e+n+t+", "Urgent"); // ?i ==> case insensitive
    System.out.println(s);
}

hitttt Urgent rrare

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM