简体   繁体   English

Java字符串拆分为字符串“},”

[英]Java string split on string “},”

I have a String that i need to split on the expression "}," I tried to do simply: 我有一个字符串,我需要拆分表达式“},”我试图简单地做:

String[] result = myString.split("},");

but it didn't work, then i tried to do: 但它没有用,然后我试着做:

Pattern p = Pattern.compile("},");
String[] result = p.split(myString);

And I got a PatternSyntaxException 我得到了一个PatternSyntaxException

Any advice please? 有什么建议吗? Thanks 谢谢

Try it again after escaping { character that is already a part of Java regex pattern . 在转义{已经是Java正则表达式模式的一部分的字符后再试一次。

String[] result = myString.split("\\},");

Pattern p = Pattern.compile("\\},");
String[] result = p.split(myString);

It's working without escaping character } as well. 它的工作没有转义字符}为好。 What is your input string? 你的输入字符串是什么?

Sample code: 示例代码:

String myString = "{abc},{lmn},{xyz}";

System.out.println(myString.split("},").length); // 3

Pattern p = Pattern.compile("},");
String[] result = p.split(myString);
System.out.println(result.length); // 3

Test your regex on DEBUGGEX - Online visual regex tester DEBUGGEX上测试你的正则表达式- 在线可视正则表达式测试器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM