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 + "]", "");
BENARD Patrick
31k16 gold badges104 silver badges109 bronze badges
1 Answer 1
You could do
String commonChars = s.replaceAll(m + "+", m);
answered Feb 10, 2014 at 13:34
Reimeus
160k16 gold badges225 silver badges282 bronze badges
Sign up to request clarification or add additional context in comments.
6 Comments
Jayesh Ahir
Thank you..but what if i also want to find the common character in two string like if s = "avfd" m = "av" than output is commonChars="av"
Prasad
@JayeshAhir It depends on how you use it. :)
Jayesh Ahir
Basicly i want find common characters in two string ..so can you suggest me regex for that..thanks
tilois
You can't use regular expressions that way. A regular expression is a single expression which matches a number of strings. It can't be used to extract common characters of multiple strings.
|
lang-java