怎么写正则?

why7777777 2010-08-24 11:21:12
<td width="31%" height=25 valign="top" > <div align="left">
ISO 22745-1-2010
</div></td>
用正则怎么取出 ISO 22745-1-2010
<td width="34%" height=25 valign="top" > <div align="left"><a href="list_standard_content.asp?stand_id=ISO@22745-1-2010" target="_blank"><font color="#000066">工业自动化系统和集成.开放技术字典及其应用于主数据.第1部分:综述与基本原则</font></a></div></td>
<td width="35%" height=25 valign="top" > <div align="left"><a href="list_standard_content.asp?stand_id=ISO@22745-1-2010" target="_blank"><font color="#000066">Industrial automation systems and integration - Open technical dictionaries and their application to master data - Part 1: Overview and fundamental principles</font></a></div></td>
用正则怎么取出width=34% 里的stand_id 会的告诉我啊 谢谢了 很急
...全文
156 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
why7777777 2010-08-27
  • 打赏
  • 举报
回复
星期1结贴
why7777777 2010-08-27
  • 打赏
  • 举报
回复
你做出来了 我也搞出来 但是我现在还有一个 问题 你可以告诉我吗 加我QQ 我就更好455255917 期待哦



<FONT color=#000066>代
替 号</FONT></div></TD>
<TD bgColor=#ffffff> ISO 15370-2001;ISO/FDIS 15370-2009</TD>

ISO 15370-2001;ISO/FDIS 15370-2009 这个我怎么写都写不出来。
magong 2010-08-25
  • 打赏
  • 举报
回复
(?<=<td width="31%" height=25 valign="top" > <div align="left">).*?(?=<\/div>)

(?<=<td width="34%" height=25 valign="top" > <div align="left"><a href="list_standard_content.asp\?stand_id=).*?(?=")
why7777777 2010-08-25
  • 打赏
  • 举报
回复
你的是对的 但是 你这样写的话 就有很多匹配的。我要正则就是下载网页上的内容 你可以把这个条件加进去吗 就是 width="31%"和另一个width=“34%” 谢谢了啊
magong 2010-08-24
  • 打赏
  • 举报
回复
唉。已经是XML了,用XPath不行么。就是DOM也中啊。
wzjmjx 2010-08-24
  • 打赏
  • 举报
回复
学习下 
closewbq 2010-08-24
  • 打赏
  • 举报
回复

String str="<td width=\"31%\" height=25 valign=\"top\" > <div align=\"left\">ISO 22745-1-2010</div></td>";
Matcher m=Pattern.compile("<div[^<>]*>([^<>]*)</div>").matcher(str);
if(m.find())
System.out.println(m.group(1).trim());
String str1="<td width=\"34%\" height=25 valign=\"top\" > <div align=\"left\"><a href=\"list_standard_content.asp?stand_id=ISO@22745-1-2010\" target=\"_blank\">";
Matcher m1=Pattern.compile("<td width=\"34%\"[^<>]*>\\s*<div[^<>]*>\\s*<a href=\"[^\"]+(stand_id=[^\"]*)\".*>").matcher(str1);
while(m1.find())
System.out.println(m1.group(1).trim());
}
magong 2010-08-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 why7777777 的回复:]

大哥 我只是想要一个匹配的正则 而不是代码啊
[/Quote]
嗨,那些regExp变量中的字符串,还有上面两位compile方法中的字符串参数不就是正则表达式吗。
如,我的,就是
(?<=left">).*?(?=<\/div>)

(?<=stand_id=).*?(?=")
JavaWorm015 2010-08-24
  • 打赏
  • 举报
回复
哎,正则表达式具体有什么用啊
why7777777 2010-08-24
  • 打赏
  • 举报
回复
大哥 我只是想要一个匹配的正则 而不是代码啊
magong 2010-08-24
  • 打赏
  • 举报
回复
Java中,
String regExp = "(?<=left\">).*?(?=<\\/div>)";
String regExp = "(?<=stand_id=).*?(?=\")";

做个参考吧,重要的是这样几点:
1、Java字符串中"要加前缀\
2、正则中/字符特殊,要加\前缀转义,java字符串中也就是加\\前缀
3、.*后加?表示尽量短的匹配
thegodofwar 2010-08-24
  • 打赏
  • 举报
回复
测试结果:

ISO 22745-1-2010
ISO@22745-1-2010
ISO@22745-1-2010
thegodofwar 2010-08-24
  • 打赏
  • 举报
回复
实际当中应该有换行呀,所以得用内嵌标志忽略换行符当做一行来处理:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextHtmlRegex {

public static void main(String[] args) {
String str1="<td width=\"31%\" height=25 valign=\"top\" > \n" +
"<div align=\"left\">\n" +
"ISO 22745-1-2010\n" +
"</div>\n"+
"</td>";
String str2="<td width=\"34%\" height=25 valign=\"top\" > \n" +
"<div align=\"left\">\n" +
"<a href=\"list_standard_content.asp?stand_id=ISO@22745-1-2010\" target=\"_blank\">\n" +
"<font color=\"#000066\">工业自动化系统和集成.开放技术字典及其应用于主数据.第1部分:综述与基本原则</font>\n" +
"</a>\n" +
"</div>\n" +
"</td>";
String str3="<td width=\"35%\" height=25 valign=\"top\" >\n" +
"<div align=\"left\">\n" +
"<a href=\"list_standard_content.asp?stand_id=ISO@22745-1-2010\" target=\"_blank\">\n" +
"<font color=\"#000066\">Industrial automation systems and integration - Open technical dictionaries and their application to master data - Part 1: Overview and fundamental principles\n" +
"</font>\n" +
"</a>\n" +
"</div>\n" +
"</td>";


Pattern p1=Pattern.compile("(?s)<td\\swidth=\"31%\".*<div.*>([^<>]+)<[/]div>.*");//(?s)的意思是忽略它后面的所有换行字符(\n)
Matcher m1=p1.matcher(str1);
if(m1.find()) {
System.out.println(m1.group(1).trim());
}

Pattern p2=Pattern.compile("(?s)<td\\swidth=\"34%\".*<a\\shref=.*[?]stand_id=([^\"<>/&]+)[\"&].*<[/]a>.*");
Matcher m2=p2.matcher(str2);
if(m2.find()) {
System.out.println(m2.group(1).trim());
}

Pattern p3=Pattern.compile("(?s)<td\\swidth=\"35%\".*<a\\shref=.*[?]stand_id=([^\"<>/&]+)[\"&].*<[/]a>.*");
Matcher m3=p3.matcher(str3);
if(m3.find()) {
System.out.println(m3.group(1).trim());
}
}

}

第二种就应该符合你的要求~~~
why7777777 2010-08-24
  • 打赏
  • 举报
回复
不是这样的。 就是写一个正则 格式是这样的

(?<= # 断言要匹配的文本的前缀


(?= # 断言要匹配的文本的后缀
) # 后缀结束

.* # 匹配任意文本
我就是不知道怎么去拼出来 ISO 22745-1-2010 就是用.*代替就可以。

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧