Jayesh Ahir
Jayesh Ahir

Reputation: 51

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 + "]", "");

Upvotes: 0

Views: 88

Answers (1)

Reimeus
Reimeus

Reputation: 159874

You could do

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

Upvotes: 1

Related Questions