如何在java中基于正则表达式拆分字符串?
我有以下资料:如何在java中基于正则表达式拆分字符串?,java,Java,我有以下资料: String s = "foo.com^null^[]"; String s1 = "bar.com^null^[{\"seen_first\":1357882827,\"seen_last\":1357882827,\"_id\":\"93.170.52.31\",\"exclude_from_publication\":false,\"locked\":false,\"agent\":\"domain_export\",\"web_published\":true,\"ve
String s = "foo.com^null^[]";
String s1 = "bar.com^null^[{\"seen_first\":1357882827,\"seen_last\":1357882827,\"_id\":\"93.170.52.31\",\"exclude_from_publication\":false,\"locked\":false,\"agent\":\"domain_export\",\"web_published\":true,\"version\":\"IPv4\"},{\"seen_first\":1357882827,\"seen_last\":1357882827,\"_id\":\"93.170.52.21\",\"exclude_from_publication\":false,\"locked\":false,\"agent\":\"domain_export\",\"web_published\":true,\"version\":\"IPv4\"}]";
注意第三个字段。。它可以是[]或json数组。
我正试图解析这些字段
这是我目前的尝试
public static void check(String s) {
String [] tokens = s.split("^");
System.out.println(tokens[0]);
System.out.println(tokens[1]);
System.out.println(tokens[2]);
if (tokens[2].trim().equals("[]")) {
System.out.println("here--> " +true);
}
System.out.println("---------");
}
我做错了什么?
^
是正则表达式中的元字符,意思是“字符串的开头”。你需要逃避它:
String [] tokens = s.split("\\^");
^
是一个特殊的正则表达式,表示行的开始。尝试转义:\\^
。你是如何得到如此奇怪的数据的?为什么不在顶层也使用JSON呢?@squiguy:Duh。。答对 了谢谢……)@Peeterka66这不全是关于rep的。我只是马上注意到了一件事:)。我不敢相信你犯了那个错误:)@Bohemian:我也是。太晚了。现在就去睡觉。