匹配以下三种情况:[[Select Postion]]
和这种
[at the [Select postiont] int]
还有
[select
]

解决方案 »

  1.   

    String regex="(\\[at the \\[Select Postion\\] int\\])|(\\[select\\])|(\\[Select Postion\\])";
      

  2.   

    我要匹配的是[[Select Postion]],  [at the [Select postiont] int], 
    [select 
    ],这三种情况,中括号中的文字是可变得。谢谢!!
      

  3.   

    String regex="(\\[.+\\[.+\\].+\\])|(\\[.+\\])|(\\[.+\\])";
      

  4.   

    String regex="(\\[.+\\[.+\\].+\\])|(\\[\\[.+\\]\\])|(\\[.+\\])";
      

  5.   

    再改一下:
    String regex="(\\[[^\\[\\]]+\\[[^\\[\\]]+\\][^\\[\\]]+\\])|(\\[\\[[^\\[\\]]+\\]\\])|(\\[[^\\[\\]]+\\])";
    下面是测试:public class Test3{
    public static void main(String[] args) {
    String regex="(\\[[^\\[\\]]+\\[[^\\[\\]]+\\][^\\[\\]]+\\])|(\\[\\[[^\\[\\]]+\\]\\])|(\\[[^\\[\\]]+\\])";
    String[] strs={
    "[123[444]344]",
    "[2345uwr]",
    "[[fjrrfg]]",
    "[fff]uuyyy",
    "[fjf[3333]]",
    "[sd[3ff]asdf]fsaf"
    };

    for(String s:strs){
    System.out.print(s);
    String isMatch=": 匹配";
    if(!s.matches(regex)){
    isMatch=": 不匹配";
    }
    System.out.println(isMatch);
    }
    }
    }结果:
    F:\java>java Test3
    [123[444]344]: 匹配
    [2345uwr]: 匹配
    [[fjrrfg]]: 匹配
    [fff]uuyyy: 不匹配
    [fjf[3333]]: 不匹配
    [sd[3ff]asdf]fsaf: 不匹配