如题,表达式为:
Test[a=2,b=3]
经过正则表达式运算
得出结果为:
a=2,b=3
如何实现?

解决方案 »

  1.   

    再把要注说的详细一点.
    字符串一定是Test开头?
    提取的值一定是用[]括起来的?
      

  2.   

    import java.util.regex.*;
    public class Test { 
        public static void main(String[] args) {    
         String str="Test[a=2,b=3]";
         String regex="Test\\[(.+)\\]";
         Matcher ma=Pattern.compile(regex).matcher(str);
         while(ma.find()){
         System.out.println(ma.group(1));
         }    }
    }
    F:\java>java Test
    a=2,b=3
      

  3.   

    谢谢各位回复,字符串不一定是Test开头!
      

  4.   


            String str="Test[a=2,b=3]"; 
            String regex="[a-zA-Z]+\\[(.+)\\]"; 
            Matcher ma=Pattern.compile(regex).matcher(str);while(ma.find()){ 
                System.out.println(ma.group(1)); 
            } 
      

  5.   

    如果[]不嵌套,并且只提取[]以内的字符,可以用:String regex="\\[(.+)\\]";代码的其它部分不变。
      

  6.   

    结贴啦,谢谢bigbug9002,lip009,最终采用最后一种回复方式!
      

  7.   


    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Test4 {
    public static void main(String[] args) {
      String str="Java[a=2,b=3]"; 
      String regex="\\[(.+)\\]"; 
            Matcher ma=Pattern.compile(regex).matcher(str);
            while(ma.find()){ 
                System.out.println(ma.group(1)); 
            }  }
    }
    不管[]外面是什么  都能提取 a=2,b=3