Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 替换一个?加上;“什么都没有”;然后再次获取原始字符串_Java_Regex_String - Fatal编程技术网

Java 替换一个?加上;“什么都没有”;然后再次获取原始字符串

Java 替换一个?加上;“什么都没有”;然后再次获取原始字符串,java,regex,string,Java,Regex,String,这是代码,我正在尝试使用正则表达式获取原始字符串 String str = "Hai ? hello : abc ^ "; str = str.replaceAll("[?]+",""); System.out.println("1"+str); String str2 = str.replaceAll("['']+","?"); System.out.println("2"+str2); 要恢复原始字符串,正则表达式应该是什么?您必须替换替换模

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

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

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

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

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

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

要恢复原始字符串,正则表达式应该是什么?您必须替换替换模式,因为您在第一次替换中丢失了信息

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);
String str = "Hai ? hello : abc ^ ";
String str1 = str.replaceAll("?","|"); 
System.out.println("1"+str1); 
String str2 = str1.replaceAll("|","?"); 
System.out.println("2"+str2);

你怎么可能回到原来的样子?你丢失了信息。(也不太清楚为什么要在这里使用正则表达式。)Str2没有在这里给出原始字符串case@KAPILPATIL-字符串
str
为原始字符串。字符串
str1
str2
根据
replaceAll
请求进行修改。