java.util.regex.PatternSyntaxException: 不匹配的关闭 ')' : 在 string.split 操作期间
java.util.regex.PatternSyntaxException: Unmatched closing ')' : during string.split operation
我正在尝试执行类似于以下的拆分:
String str = "({Somestring 1 with a lot of braces and commas}),({Somestring 12with a lot of braces and commas})";
println str.split("}),({");
但是我看到了:
java.util.regex.PatternSyntaxException: Unmatched closing ')' near index 0
}),({
很明显,我的字符串被视为正则表达式。
有什么办法可以转义这个字符串吗?
字符(
和)
以及{
和}
是正则表达式中的特殊字符。你必须逃避这些:
println str.split("\}\),\(\{");
除了手动转义字符串,您还可以将其视为文字而不是正则表达式:
println str.split(Pattern.quote("}),({"));
在regular expressions
中必须是escaped
的Java characters
是:
.[]{}()*+-?^$|
public static void main(String[] args) {
String str = "({Somestring 1 with a lot of braces and commas}),({Somestring 12with a lot of braces and commas})";
String[] array = str.split("\}\),\(\{");
System.out.println(array.length);
}
我正在尝试执行类似于以下的拆分:
String str = "({Somestring 1 with a lot of braces and commas}),({Somestring 12with a lot of braces and commas})";
println str.split("}),({");
但是我看到了:
java.util.regex.PatternSyntaxException: Unmatched closing ')' near index 0 }),({
很明显,我的字符串被视为正则表达式。
有什么办法可以转义这个字符串吗?
字符(
和)
以及{
和}
是正则表达式中的特殊字符。你必须逃避这些:
println str.split("\}\),\(\{");
除了手动转义字符串,您还可以将其视为文字而不是正则表达式:
println str.split(Pattern.quote("}),({"));
regular expressions
中必须是escaped
的Java characters
是:
.[]{}()*+-?^$|
public static void main(String[] args) {
String str = "({Somestring 1 with a lot of braces and commas}),({Somestring 12with a lot of braces and commas})";
String[] array = str.split("\}\),\(\{");
System.out.println(array.length);
}