正则表达式获取网页<meta name="description" content=内容

前端之家收集整理的这篇文章主要介绍了正则表达式获取网页<meta name="description" content=内容前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test8 { static int tempa = 10; public static void main(String[] args) throws IOException { File file = new File("D:/2cd99ecc9a3b8f2f233cb6d9c17a86b9.jsp"); String htmlReport = readFileAsString(file); //System.out.println(htmlReport); //String pattern = "<Meta name=\"description\" content=\"(.+?)\r\n(.+?)/>"; String pattern = "name=\"description\" content=\"(.+?)/>"; Pattern p = Pattern.compile(pattern,Pattern.CASE_INSENSITIVE | Pattern.DOTALL); Matcher matcher = p.matcher(htmlReport); while (matcher.find()) { String tmp = matcher.group(1); System.out.println(tmp); } } private static String readFileAsString(File file) throws IOException { StringBuffer strBuff = new StringBuffer(); String charsetName = "utf-8"; if(file != null && file.exists()){ //InputStream in = new FileInputStream(file); InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file),charsetName); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String line = null; while((line = bufferedReader.readLine()) != null){ strBuff.append(line); strBuff.append("\n"); } inputStreamReader.close(); bufferedReader.close(); } else { } return strBuff.toString(); /*char[] chr = new char[4096]; final StringBuffer buffer = new StringBuffer(); final FileReader reader = new FileReader(file); try { while ((len = reader.read(chr)) > 0) { buffer.append(chr,len); System.out.println("len=======" + len); } } finally { reader.close(); }*/ } }

猜你在找的正则表达式相关文章