java正则表达式匹配所有相匹配的内容

版权声明: https://blog.csdn.net/qq_40244755/article/details/80957258

java使用正则表达式匹配所有内容
----

**
构建正则表达式
String patternCode="baseid=\\w+";
String patternTitle="title=\\\"[\\u4e00-\\u9fa5]*·?[\\u4e00-\\u9fa5]*\\(?[\\u4e00-\\u9fa5A-Za-z\\s]*\\)?[^\\x00-\\xff]?\\\"";
Pattern pCode=Pattern.compile(patternCode);
Pattern pTitle=Pattern.compile(patternTitle);
Matcher mCode=pCode.matcher(resultData);
Matcher mTitle=pTitle.matcher(resultData);
//构建数组存储数据
List<String> codeList=new ArrayList<>();
List<String> titleList=new ArrayList<>();

匹配内容,这里我使用了两种方式,一种能得到,一种不能得到,一直没想明白,po上来。

不能匹配到内容的:

if(mCode.find){
for(int i=0;i<mCode.groupCount;i++){
codeList.add(mCode.group());
}
}

能匹配到所有内容的:

while (mCode.find()){
codeList.add(mCode.group());
}
while (mTitle.find()){
titleList.add(mTitle.group());
}

知道原因的,还请告诉我~

猜你喜欢

转载自blog.csdn.net/qq_40244755/article/details/80957258