求一条正则表达式的写法 本帖最后由 u014301433 于 2014-05-05 10:06:44 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 对正则表达式不是很熟悉,不过写了个示例,自己参考下吧:示例:import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;import java.util.regex.Matcher;import java.util.regex.Pattern;public class RexTest { public static void main(String[] args) { String str = getOneHtml("E:/test.html"); Pattern p = Pattern.compile("(?<=<div class=\"RTit clearfix\"><em>).*(?=</em><span class=\"zjTit cGray\">)"); Matcher m = p.matcher(str); while (m.find()) System.out.println(m.group()); } public static String getOneHtml(String htmlFile){ String temp; StringBuffer sb = new StringBuffer(); try { File file = new File(htmlFile); BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8")); while ((temp = in.readLine()) != null) { sb.append(temp.trim()); } in.close(); }catch (Exception e){ e.printStackTrace(); } return sb.toString(); }}<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><div class="RTit clearfix"> <em>掌趣科技(300315)分红派息记录</em> <span class="zjTit cGray"><a href='tab_fhpxya.php?symbol=300315'>分红派息预案</a></span> </div> <table width="706" border="0" cellspacing="0" cellpadding="0" class="tab01"> </table></body></html> 对正则表达式不是很熟悉,不过写了个示例,自己参考下吧:示例:import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;import java.util.regex.Matcher;import java.util.regex.Pattern;public class RexTest { public static void main(String[] args) { String str = getOneHtml("E:/test.html"); Pattern p = Pattern.compile("(?<=<div class=\"RTit clearfix\"><em>).*(?=</em><span class=\"zjTit cGray\">)"); Matcher m = p.matcher(str); while (m.find()) System.out.println(m.group()); } public static String getOneHtml(String htmlFile){ String temp; StringBuffer sb = new StringBuffer(); try { File file = new File(htmlFile); BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8")); while ((temp = in.readLine()) != null) { sb.append(temp.trim()); } in.close(); }catch (Exception e){ e.printStackTrace(); } return sb.toString(); }}<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><div class="RTit clearfix"> <em>掌趣科技(300315)分红派息记录</em> <span class="zjTit cGray"><a href='tab_fhpxya.php?symbol=300315'>分红派息预案</a></span> </div> <table width="706" border="0" cellspacing="0" cellpadding="0" class="tab01"> </table></body></html>你的代码用截取的网页源码片断测试可以,但输入完整的URL测试不成功。我的测试URL是:http://app.finance.ifeng.com/data/stock/tab_fhpxjl.php?symbol=300315麻烦你帮我看下 对正则表达式不是很熟悉,不过写了个示例,自己参考下吧:示例:import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;import java.util.regex.Matcher;import java.util.regex.Pattern;public class RexTest { public static void main(String[] args) { String str = getOneHtml("E:/test.html"); Pattern p = Pattern.compile("(?<=<div class=\"RTit clearfix\"><em>).*(?=</em><span class=\"zjTit cGray\">)"); Matcher m = p.matcher(str); while (m.find()) System.out.println(m.group()); } public static String getOneHtml(String htmlFile){ String temp; StringBuffer sb = new StringBuffer(); try { File file = new File(htmlFile); BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8")); while ((temp = in.readLine()) != null) { sb.append(temp.trim()); } in.close(); }catch (Exception e){ e.printStackTrace(); } return sb.toString(); }}<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><div class="RTit clearfix"> <em>掌趣科技(300315)分红派息记录</em> <span class="zjTit cGray"><a href='tab_fhpxya.php?symbol=300315'>分红派息预案</a></span> </div> <table width="706" border="0" cellspacing="0" cellpadding="0" class="tab01"> </table></body></html>你的代码用截取的网页源码片断测试可以,但输入完整的URL测试不成功。我的测试URL是:http://app.finance.ifeng.com/data/stock/tab_fhpxjl.php?symbol=300315麻烦你帮我看下你改下程序就可以访问网页,你用下面的方法替换:public static String getHtmlFromURL(String htmlFile){ URL url; String temp; StringBuffer sb = new StringBuffer(); try { url = new URL(htmlFile); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8")); while ((temp = in.readLine()) != null) { sb.append(temp.trim()); } in.close(); }catch (Exception e){ e.printStackTrace(); } return sb.toString(); } java基本类型的转换 怎么用Java实现定时执行某程序 文件移动的问题,感激不尽!多谢了! 哪出错了? [提问]如何解决JTextField中文输入异常? 为什么我的网站在在我的电脑上IIS测试没有问题,上传到空间就打不开。请高手指教,给出详细的解决方案,我是新手!我的空间网址是www.hswcn.com ***急问!利用Java如何得到一个文件的属性信息例如:-rw-rw-rw-?*** 那位朋友可以给我发一份SCJP的最新考试真题?100分 初始化的问题 如何制作java的“exe” Java中创建数组的问题 如何从数组中随机获取元素
示例:import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class RexTest { public static void main(String[] args) {
String str = getOneHtml("E:/test.html");
Pattern p = Pattern.compile("(?<=<div class=\"RTit clearfix\"><em>).*(?=</em><span class=\"zjTit cGray\">)");
Matcher m = p.matcher(str);
while (m.find())
System.out.println(m.group()); } public static String getOneHtml(String htmlFile){
String temp;
StringBuffer sb = new StringBuffer();
try {
File file = new File(htmlFile);
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
while ((temp = in.readLine()) != null) {
sb.append(temp.trim());
}
in.close();
}catch (Exception e){
e.printStackTrace();
}
return sb.toString();
}}<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div class="RTit clearfix">
<em>掌趣科技(300315)分红派息记录</em>
<span class="zjTit cGray"><a href='tab_fhpxya.php?symbol=300315'>分红派息预案</a></span>
</div>
<table width="706" border="0" cellspacing="0" cellpadding="0" class="tab01">
</table>
</body>
</html>
示例:import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class RexTest { public static void main(String[] args) {
String str = getOneHtml("E:/test.html");
Pattern p = Pattern.compile("(?<=<div class=\"RTit clearfix\"><em>).*(?=</em><span class=\"zjTit cGray\">)");
Matcher m = p.matcher(str);
while (m.find())
System.out.println(m.group()); } public static String getOneHtml(String htmlFile){
String temp;
StringBuffer sb = new StringBuffer();
try {
File file = new File(htmlFile);
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
while ((temp = in.readLine()) != null) {
sb.append(temp.trim());
}
in.close();
}catch (Exception e){
e.printStackTrace();
}
return sb.toString();
}}<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div class="RTit clearfix">
<em>掌趣科技(300315)分红派息记录</em>
<span class="zjTit cGray"><a href='tab_fhpxya.php?symbol=300315'>分红派息预案</a></span>
</div>
<table width="706" border="0" cellspacing="0" cellpadding="0" class="tab01">
</table>
</body>
</html>
你的代码用截取的网页源码片断测试可以,但输入完整的URL测试不成功。
我的测试URL是:http://app.finance.ifeng.com/data/stock/tab_fhpxjl.php?symbol=300315
麻烦你帮我看下
示例:import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class RexTest { public static void main(String[] args) {
String str = getOneHtml("E:/test.html");
Pattern p = Pattern.compile("(?<=<div class=\"RTit clearfix\"><em>).*(?=</em><span class=\"zjTit cGray\">)");
Matcher m = p.matcher(str);
while (m.find())
System.out.println(m.group()); } public static String getOneHtml(String htmlFile){
String temp;
StringBuffer sb = new StringBuffer();
try {
File file = new File(htmlFile);
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
while ((temp = in.readLine()) != null) {
sb.append(temp.trim());
}
in.close();
}catch (Exception e){
e.printStackTrace();
}
return sb.toString();
}}<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div class="RTit clearfix">
<em>掌趣科技(300315)分红派息记录</em>
<span class="zjTit cGray"><a href='tab_fhpxya.php?symbol=300315'>分红派息预案</a></span>
</div>
<table width="706" border="0" cellspacing="0" cellpadding="0" class="tab01">
</table>
</body>
</html>
你的代码用截取的网页源码片断测试可以,但输入完整的URL测试不成功。
我的测试URL是:http://app.finance.ifeng.com/data/stock/tab_fhpxjl.php?symbol=300315
麻烦你帮我看下你改下程序就可以访问网页,你用下面的方法替换:public static String getHtmlFromURL(String htmlFile){
URL url;
String temp;
StringBuffer sb = new StringBuffer();
try {
url = new URL(htmlFile);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
while ((temp = in.readLine()) != null) {
sb.append(temp.trim());
}
in.close();
}catch (Exception e){
e.printStackTrace();
}
return sb.toString();
}