如何用正则表达式提取值 Test[a=2,b=3] 为 a=2,b=3 如题,表达式为:Test[a=2,b=3]经过正则表达式运算得出结果为:a=2,b=3如何实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 再把要注说的详细一点.字符串一定是Test开头?提取的值一定是用[]括起来的? 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 Testa=2,b=3 谢谢各位回复,字符串不一定是Test开头! 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)); } 如果[]不嵌套,并且只提取[]以内的字符,可以用:String regex="\\[(.+)\\]";代码的其它部分不变。 结贴啦,谢谢bigbug9002,lip009,最终采用最后一种回复方式! 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 linux 下运行java程序问题 SWT中有没有颜色选择器? JAVA swing用liquidlnf包模拟MAC界面的问题 关于super的用法的一个出错问题! 简单问题,谁帮我解决? 如何实现文件夹的解压缩? java 中文问题 奇怪???? Jbuilder的问题! ecplise导入新项目的问题 请教关于toString()方法的问题 关于垃圾回收?
字符串一定是Test开头?
提取的值一定是用[]括起来的?
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
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));
}
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