-4

这是代码,我正在尝试使用正则表达式来获取原始字符串

        String str = "Hai ? hello : abc ^ ";

   str = str.replaceAll("[?]+","");

   System.out.println("1"+str);

   String str2 = str.replaceAll("['']+","?");

   System.out.println("2"+str2);

取回原始字符串的正则表达式应该是什么

4

2 回答 2

1
String str = "Hai ? hello : abc ^ ";
String str1 = str.replaceAll("[?]+",""); 
System.out.println("1"+str1); 
String str2 = str1.replaceAll("['']+","?"); 
System.out.println("2"+str2);
System.out.println(str);
于 2012-07-03T17:21:38.843 回答
0

您必须替换替换模式,因为您在第一次替换时丢失了信息

String str = "Hai ? hello : abc ^ ";
String str1 = str.replaceAll("?","|"); 
System.out.println("1"+str1); 
String str2 = str1.replaceAll("|","?"); 
System.out.println("2"+str2);
于 2012-07-05T21:09:30.633 回答