I want to replace "{
from below String
:
public static void main(String args[]){
String input="Subtitle,\"{\"key\": \"IsReprint\", \"value\":\"COPY\"}";
input=input.replaceAll("\"{", "{"));
System.out.println("String ::::"+input);
}
I'm getting this error:
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition near index 1
\"{
^
You have two ways:
The first you have to escape the {
with \\\\{
, because replaceAll use regex so you have to escape the "
and {
:
input=input.replaceAll("\"\\{", "{");
The second is to use replace
instead if you don't have this complicated regex :
input=input.replace("\"{", "{");
replaceAll
takes a regex as an argument. {
has a special meaning in regex, so have to escape {
by doing
input=input.replaceAll("\"\\{", "{");
or use replace
, which doesn't take a regex as an argument.
input=input.replace("\"{", "{");
You are not escaping the "{" character correctly when you are calling replaceAll.
You need to use "two slashes" \\\\ before any regular expression (regExp).
Here is an example:
public static void main(String args[]){
String input="Subtitle,\"{\"key\": \"IsReprint\", \"value\":\"COPY\"}";
System.out.println(input.replaceAll("\\{", "*"));
}
My example replaces the "{" character with a *:
"\\{", "*"
Running , you get the ouput:
Subtitle,"*"key": "IsReprint", "value":"COPY"}
With the input:
String input="{{ }}";
You get the output:
** }}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.