java使用pattern和Matcher获取html代码中的一串字符,主要是使用正则表达式来匹配html的标签,如下代码:
package com.qiu.lin.he;
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Ceshi {
public static void main(String[] args) throws ParseException {
String string = "75757574
12312341243 ";
Pattern pattern = Pattern.compile(">([\\d]+)<");//匹配html字符
Matcher matcher = pattern.matcher(string);
if (matcher.find()) {
matcher.reset();
while (matcher.find()) {//找到匹配的字符串
System.out.println("hit: " + matcher.group(1));
}
} else {
System.out.println("[ERROR] NOT FOUND!");
}
}
}
结果如下:
![](https://iknow-pic.cdn.bcebos.com/0b46f21fbe096b634301e0b60a338744ebf8ac38?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)