String str = "[{name:\"孙悟空\",sex:\"male\",age:18,job:\"student\"}," +
 "{name:\"周星驰\",sex:\"male\",age:18,job:\"student\"}," +
 "{name:\"周星星\",sex:\"male\",age:18,job:\"student\"}]";
我如何用正则表达式分割成{name:\"孙悟空\",sex:\"male\",age:18,job:\"student\"}这样子的
很急  求帮忙  我正则表达式学的不太好

解决方案 »

  1.   

    貌似还不能直接去切这个字符串只能是用正则去匹配有多少个匹配的子串 这个样子。。获得的子串就是你需要的结果了这个写你自己需要的字符串的正则很容易的http://developer.51cto.com/art/201003/188828.htm
    参考这里应该很容易解决了!呵呵
      

  2.   


    String str = "[{name:\"孙悟空\",sex:\"male\",age:18,job:\"student\"}," +
     "{name:\"周星驰\",sex:\"male\",age:18,job:\"student\"}," +
     "{name:\"周星星\",sex:\"male\",age:18,job:\"student\"}]";

    //方法一:用正则匹配
    Matcher m = Pattern.compile("(\\{[^\\}]+})").matcher(str);
    while(m.find()){
    System.out.println(m.group(1));
    }

    //方法二:用正则分割
    str = str.substring(1, str.length()-1); //去掉两边的中括号
    String arr [] = str.split("(?<=\\}),(?=\\{)");
    for(String item:arr){
    System.out.println(item);
    }
      

  3.   

    再提供一种:String str = "[{name:\"孙悟空\",sex:\"male\",age:18,job:\"student\"}," +
             "{name:\"周星驰\",sex:\"male\",age:18,job:\"student\"}," +
             "{name:\"周星星\",sex:\"male\",age:18,job:\"student\"}]";
    Scanner sc = new Scanner(str);
    while (sc.hasNext()) {
        System.out.println(sc.findInLine("\\{[^\\}]+\\}"));
    }