簡體   English   中英

無法解析JAVA中的正則表達式

[英]Unable to Parse reqular expression in JAVA

這是我的代碼

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但沒有運氣

在工作的小提琴中檢查

使用正則表達式如下

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

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

這就像魅力一樣起作用,並且此鏈接也幫助我很多http://regexr.com/

感謝您的所有投入!

該塊的輸出是: {"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("");
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM