String s1 = "|"; // 輸出 | System.out.println(s1); String s2 = s1.replaceAll("|","1"); // 輸出 1|1 System.out.println(s2); String s3 = "<mfenced separators=\"|\">"; String s4 = s3.replaceAll("<mfenced separators=\"|\">","2"); // 這個輸出 2|2 System.out.println(s4); // 因此之後複雜的公式匹配 | ,就先將|替換掉,再進行匹配 String s5 = s1.replaceAll("\\|", "1"); // 輸出 1 System.out.println(s5); String s6 = s3.replaceAll("<mfenced separators=\"\\|\">", "2"); // 輸出 <mfenced separators="|"> System.out.println(s3); // 若是想將s3替換爲2,如何操做 String s7 = s3.replaceAll("\\|", "豎"); s7 = s7.replaceAll("<mfenced separators=\"豎\">","2"); // 輸出 2 System.out.println(s7);