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!");
}
}
}
结果如下: