String testString ="var D_Room1 = new Array (252,523,230,61);" ;
Pattern pattern = Patterncompile("\\(\\)");
Matcher matcher = patternmatcher(testString);
while(matcherfind())
{
Systemoutprintln(matchergroup()); //打印
}
比如上,可以得到结果(252,523,230,61)。剩下该咋咋地
1就用LS的例子来改进吧,LS的例子用正则匹配的捕获型,也就是说除了捕获了/patroninfo~S0chx/1069163/modpinfo,还捕获了带单引号的 '/patroninfo~S0chx/1069163/modpinfo‘ (输出 Systemoutprintln(matgroup(0) 就是);
这样无论在效率还是安全性上都有欠缺。
2 Patterncompile("\\'()\\'")也可以改为Pattern pat = Patterncompile("\'()\'"),减少代码冗余和理解复杂度。
捕获用法:
public class Test {
public static void main(String[] args) {
String str = "<a href=\"#\" onClick=\"return open_new_window( '/patroninfo~S0chx/1069163/modpinfo' )\">";
Pattern pat = Patterncompile("\'()\'");
Matcher mat = patmatcher(str);
if (matfind()) {
Systemoutprintln(matgroup(1));
}
}
}
非捕获方法:
public class Test {
public static void main(String[] args) {
String str = "<a href=\"#\" onClick=\"return open_new_window( '/patroninfo~S0chx/1069163/modpinfo' )\">";
Pattern pat = Patterncompile("(<=\')(=\')");
Matcher mat = patmatcher(str);
if (matfind()) {
Systemoutprintln(matgroup(0));
}
}
}
以上就是关于java语言利用正则表达式获取数组,急求!!全部的内容,包括:java语言利用正则表达式获取数组,急求!!、求助 java正则表达式将括号里的内容提取出来、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)