有这么一字符串例:String str = "<script>parent.setGoalAlertStatus(false);parent.od_Data[1].s = [4303];parent.od_Data[1].c = [[[4,'ITALY SERIE A',0],    [      [722901,'Brescia','Inter Milan','1.010',6,'03/12/2011 03:45',0,4,1,0,'SG: CH202&#13;MY: ASTRO816&#13;IDN]]]]];have more chars";
//字符串的内容是多变的,这里只是举个例子。
想求得以 [[[ 开头 ]]]]]结尾 子字符串。以上面为例,我要取得[[4,'ITALY SERIE A',0],    [      [722901,'Brescia','Inter Milan','1.010',6,'03/12/2011 03:45',0,4,1,0,'SG: CH202&#13;MY: ASTRO816&#13;IDN]]]]] 这么一段

解决方案 »

  1.   

    String str = "<script>parent.setGoalAlertStatus(false);parent.od_Data[1].s = [4303];parent.od_Data[1].c = [[[4,'ITALY SERIE A',0],    [      [722901,'Brescia','Inter Milan','1.010',6,'03/12/2011 03:45',0,4,1,0,'SG: CH202&#13;MY: ASTRO816&#13;IDN]]]]];have more chars";
    Pattern p = Pattern.compile(".+(\\[\\[.+\\]\\]\\]\\]\\]).+");
    Matcher m = p.matcher(str);
    while(m.find()){
    System.out.println(m.group(1));
    }
      

  2.   

    分析一下LZ的需求,单个[]内的匹配时不合格的,[ ]左端或者有段的括号至少要有2个,但并不规律。
    public static void main(String[] args) {
    String str = "<script>parent.setGoalAlertStatus(false);parent.od_Data[1].s = " +
    "[4303];parent.od_Data[1].c = [[[4,'ITALY SERIE A',0],    [     [722901" +
    ",'Brescia','Inter Milan','1.010',6,'03/12/2011 03:45',0,4,1,0,'SG: CH202&" +
    "#13;MY: ASTRO816&#13;IDN]]]]];have more chars";
        
    String regex = "\\[{2,}.*\\]{2,}";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(str);
    while(m.find()) {
    System.out.println(m.group());
    }
    }