java正则表达式取括号里面的内容

public static String changeCompName(String compName){
            String NewCompName="";
            
            //cm1230NHL6X7K(2)
            //String tests="cm1230NHL6X7K(2)";
            //compName=tests;      
                        
            boolean twice=false;
            if(compName.indexOf("(")>=0) twice=true;
            Pattern p=Pattern.compile("([a-z]+)(\\d+)");
            Matcher m=p.matcher(compName);
            while(m.find()) {
                NewCompName=m.group();
            }
 
            if(twice){
                Pattern p2=Pattern.compile("(?<=\\()[^\\)]+");
                Matcher m2=p2.matcher(compName);
                while(m2.find()) {
                    NewCompName+="("+m2.group()+")";
                }
            }
             
            //System.out.println(NewCompName);  //cm1230(2) 
 return NewCompName; }

 

posted on 2016-01-27 14:51  rojas  阅读(3358)  评论(0编辑  收藏  举报