简体   繁体   中英

java regular expression with replaceAll on string

I have two strings :

s = "aaaaaaa" and m = "a"

I want output as commonChars="a" but i am getting commonChars="aaaaaaa" and for s = "a" m = "aaaa"

I want output commonChars="a"

Can anyone suggest me regular expression for that ?

My code is

String commonChars = s.replaceAll("[^" + m + "]", "");

你可以做

String commonChars = s.replaceAll(m + "+", m);

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