如何使用java的正则表达式提取html标签
通过如下的java方法取得了htmlpublicStringgetHtml(StringurlString){try{StringBufferhtml=newString...
通过如下的java方法取得了html
public String getHtml(String urlString) {
try {
StringBuffer html = new StringBuffer();
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStreamReader isr = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(isr);
String temp;
while ((temp = br.readLine()) != null) {
html.append(temp).append("\n");
}
br.close();
isr.close();
return html.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
我想从中取得html标签"<title>XXXX</title>"中的文字XXXX,请各位高手指点一下,正则表达式应该如何写? 谢谢啦!!!!!! 展开
public String getHtml(String urlString) {
try {
StringBuffer html = new StringBuffer();
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStreamReader isr = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(isr);
String temp;
while ((temp = br.readLine()) != null) {
html.append(temp).append("\n");
}
br.close();
isr.close();
return html.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
我想从中取得html标签"<title>XXXX</title>"中的文字XXXX,请各位高手指点一下,正则表达式应该如何写? 谢谢啦!!!!!! 展开
展开全部
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String args[]) {
String html = "<title>ABCD</title>gsdggas<title></title>jkll<title>005</title>";
// 简单示例,相当于String html=getHtml(String urlString);
List resultList = getContext(html);
for (Iterator iterator = resultList.iterator(); iterator.hasNext();) {
String context = (String) iterator.next();
System.out.println(context);
}
}
/**
* 提取"<title>XXXX</title>"中的文字XXXX
* @param html 要解析的html文档内容
* @return 解析结果,可以多次匹配,每次匹配的结果按文档中出现的先后顺序添加进结果List
*/
public static List getContext(String html) {
List resultList = new ArrayList();
Pattern p = Pattern.compile("<title>([^</title>]*)");//匹配<title>开头,</title>结尾的文档
Matcher m = p.matcher(html );//开始编译
while (m.find()) {
resultList.add(m.group(1));//获取被匹配的部分
}
return resultList;
}
}
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String args[]) {
String html = "<title>ABCD</title>gsdggas<title></title>jkll<title>005</title>";
// 简单示例,相当于String html=getHtml(String urlString);
List resultList = getContext(html);
for (Iterator iterator = resultList.iterator(); iterator.hasNext();) {
String context = (String) iterator.next();
System.out.println(context);
}
}
/**
* 提取"<title>XXXX</title>"中的文字XXXX
* @param html 要解析的html文档内容
* @return 解析结果,可以多次匹配,每次匹配的结果按文档中出现的先后顺序添加进结果List
*/
public static List getContext(String html) {
List resultList = new ArrayList();
Pattern p = Pattern.compile("<title>([^</title>]*)");//匹配<title>开头,</title>结尾的文档
Matcher m = p.matcher(html );//开始编译
while (m.find()) {
resultList.add(m.group(1));//获取被匹配的部分
}
return resultList;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询