0

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

1 Answer 1

1

You could do

String commonChars = s.replaceAll(m + "+", m);
Sign up to request clarification or add additional context in comments.

6 Comments

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"
@JayeshAhir It depends on how you use it. :)
Basicly i want find common characters in two string ..so can you suggest me regex for that..thanks
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.
Indeed see this post to see how problematic that can be
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.