Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.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
PatternSyntaxException:String.replaceAll()android_Android_Replace - Fatal编程技术网

PatternSyntaxException:String.replaceAll()android

PatternSyntaxException:String.replaceAll()android,android,replace,Android,Replace,我想删除所有{},如下所示: String regex = getData.replaceAll("{", "").replaceAll("}", ""); 但强制关闭我的应用程序与日志 java.util.regex.PatternSyntaxException: Syntax error U_REGEX_RULE_SYNTAX 我做错了什么?你需要逃避{: String regex = getData.replaceAll("\\{", "").replaceAll("\\}", "")

我想删除所有{},如下所示:

String regex = getData.replaceAll("{", "").replaceAll("}", "");
但强制关闭我的应用程序与日志

java.util.regex.PatternSyntaxException: Syntax error U_REGEX_RULE_SYNTAX

我做错了什么?

你需要逃避
{

String regex = getData.replaceAll("\\{", "").replaceAll("\\}", "");

花括号用于指定正则表达式中的重复,因此必须对其进行转义

此外,还应该考虑一次删除所有括号,而不是两次调用RetryPalt(String,String)。


对于要执行的操作,不需要使用正则表达式

您可以使用
replace
方法来匹配特定字符,这会稍微增加可读性:


转义
\{
只是为了能够使用
replaceAll
有效,但在您的情况下没有意义

{不是有效的regexp.works,但不需要使用正则表达式来匹配普通字符
String regex = getData.replaceAll("\\{|\\}", "");
String regex = getData.replace("{", "").replace("}", "");