您的位置:首页 > 编程语言 > Java开发

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

2016-01-27 14:51 337 查看
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; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: