无法在JAVA中解析reqular表达式

时间:2016-02-04 14:41:37

标签: java regex

这是我的代码,

String xyz = "{\"status\":\"ok\",\"data\":[{\"RatingCount\":4}],         [{\"RatingCount\":1}], [{\"RatingCount\":1}]\"code\":1}";
    String pattern = ".*],\\s*\\[.*";//"(.*)(],\\s.*\\[)(.*)";
    Pattern p1 = Pattern.compile(pattern);
    Matcher m1 = p1.matcher(xyz);
    boolean b = m1.matches();
    System.out.println(b);

我想将模式'], ['替换为""

我使用了replaceAll但没有运气

3 个答案:

答案 0 :(得分:0)

Check it in working Fiddle

使用以下正则表达式

String pattern = "\],[ ]*\[";

答案 1 :(得分:0)

xyz = xyz.replaceAll(“] \ s *,\ s * \ [”,“],[”);

这就像魅力一样,这个链接也帮助了我很多http://regexr.com/

感谢您的所有投入!

答案 2 :(得分:0)

此块的输出为:{"status":"ok","data":[{"RatingCount":4}{"RatingCount":1}{"RatingCount":1}]"code":1}

public static void main(String[] args) {
    String str = "{\"status\":\"ok\",\"data\":[{\"RatingCount\":4}],         [{\"RatingCount\":1}], [{\"RatingCount\":1}]\"code\":1}";
    System.out.println(replace(str, "\\],\\s+\\["));
  }

  public static String replace(String str, String regex) {
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(str);
    return matcher.replaceAll("");
  }