How do I find the characters common to two strings in Java using single replaceAll?

后端 未结 4 736
时光说笑
时光说笑 2021-01-12 16:25

So suppose I have:

String s = \"1479K\";
String t = \"459LP\";

and I want to return

String commonChars = \"49\";

4条回答
  •  醉酒成梦
    2021-01-12 17:18

    The accepted answer:

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

    has a bug!!!

    What if the string t has a regex meta-character? In that case the replaceAll fails.

    See this program as an example where the string t has ] in it and ] is a regex meta-character which marks the end of the character class. Clearly the program does not produce the expected output.

    Why?

    Consider:

    String s = "1479K";
    String t = "459LP]";
    

    Now the regex will become(just substitute t):

    String commonChars = s.replaceAll("[^459LP]]","");
    

    Which says replace any character other than 4,5,9,L,P followed by a ] with nothing. Which is clearly not what you want.

    To fix these you need to escape the ] in t. You can do it manually as:

    String t = "459LP\\]";
    

    and the regex works fine.

    This is a common problem when using regex, so the java.util.regex.Pattern class provides a static method named quote which can be used to do exactly this: quote the regex-metacharacters so that they are treated literally.

    So before using t in replaceAll you quote it as:

    t = Pattern.quote(t);
    

    Program using quote method works as expected.

提交回复
热议问题