String str ="<a href=\"http://news.bai-du.com.cn/ns?cl=2&rn=20&tn=news&word=a\">体 育</a>"
+ "<a href=\"http://news.aa.com/ns?cl=2&rn=20&tn=news&word=a\">新闻</a>";如题~~我现在的需求是用正则表达式分别获得 字符串str中的两个链接的文本,
要示的打印结果如下
<a href=\"http://news.bai-du.com.cn/ns?cl=2&rn=20&tn=news&word=a\">体 育</a>"
"<a href=\"http://news.aa.com/ns?cl=2&rn=20&tn=news&word=a\">新闻</a>
但现在我这与的代码却因为"体 育"两个字的中间有一个空白符(可以是空格,tab,回车等),所以第一句匹配不到~~只能匹配到第二句(新闻的那一行代码),请问我应该怎么写正则表达式来解决呢?
我的代码如下:
import java.util.regex.*;public class Regex {
Regex() {
}
public static void main(String[] args) {
String str ="<a href=\"http://news.bai-du.com.cn/ns?cl=2&rn=20&tn=news&word=a\">体 育</a>"+ "<a href=\"http://news.aa.com/ns?cl=2&rn=20&tn=news&word=a\">新闻</a>";
Matcher m1 = Pattern.compile("<a href(\\S+)</a>").matcher(str);
while(m1.find()) {
for (int i = 0; i<m1.groupCount(); i++) {
System.out.println (m1.group()); }
}
}
}
+ "<a href=\"http://news.aa.com/ns?cl=2&rn=20&tn=news&word=a\">新闻</a>";如题~~我现在的需求是用正则表达式分别获得 字符串str中的两个链接的文本,
要示的打印结果如下
<a href=\"http://news.bai-du.com.cn/ns?cl=2&rn=20&tn=news&word=a\">体 育</a>"
"<a href=\"http://news.aa.com/ns?cl=2&rn=20&tn=news&word=a\">新闻</a>
但现在我这与的代码却因为"体 育"两个字的中间有一个空白符(可以是空格,tab,回车等),所以第一句匹配不到~~只能匹配到第二句(新闻的那一行代码),请问我应该怎么写正则表达式来解决呢?
我的代码如下:
import java.util.regex.*;public class Regex {
Regex() {
}
public static void main(String[] args) {
String str ="<a href=\"http://news.bai-du.com.cn/ns?cl=2&rn=20&tn=news&word=a\">体 育</a>"+ "<a href=\"http://news.aa.com/ns?cl=2&rn=20&tn=news&word=a\">新闻</a>";
Matcher m1 = Pattern.compile("<a href(\\S+)</a>").matcher(str);
while(m1.find()) {
for (int i = 0; i<m1.groupCount(); i++) {
System.out.println (m1.group()); }
}
}
}
解决方案 »
- Set集合唯一性判断依据equals有什么实际意义?
- 求助java中日期转换成String类型
- java -Djava.ext.dirs=lib -cp . TestClient //-Djava.ext.dirs=lib 是做什么用的 ??
- JTextArea文本监听器
- 我是JAVA初学者,问一个弱弱的问题
- 初学者问(问题很简单,这分很容易得)
- 这个要带参数执行的java,在JB9中怎样运行?
- 为什么我的Ie6。0仍旧不能看到 applet的程序
- 大家,一个关于学习处理异常时候遇到的学习问题
- 猴子吃桃 到现在还在流行
- 我是初学者,一个java程序,有点问题,我没检查出来,哪位能帮我看看?
- itext 怎么访问一个PDF文件啊
+ "<a href=\"http://news.aa.com/ns?cl=2&rn=20&tn=news&word=a\">新闻</a>";
Pattern p = Pattern.compile("<a[^<]+</a>");
Matcher m = p.matcher(str);
while (m.find()) {
System.out.println(str.substring(m.start(), m.end()));
}out =>
<a href="http://news.bai-du.com.cn/ns?cl=2&rn=20&tn=news&word=a">体 育</a>
<a href="http://news.aa.com/ns?cl=2&rn=20&tn=news&word=a">新闻</a>
一样代码
String str = "<a href=\"http://news.bai-du.com.cn/ns?cl=2&rn=20&tn=news&word=a\">体 育(好啊,加油!)</a>"
+ "<a href=\"http://news.aa.com/ns?cl=2&rn=20&tn=news&word=a\">新闻(fdsafd!$&*#&^&@s)</a>";
Pattern p = Pattern.compile("<a[^<]+</a>");
Matcher m = p.matcher(str);
while (m.find()) {
System.out.println(str.substring(m.start(), m.end()));
}运行输出 =>
<a href="http://news.bai-du.com.cn/ns?cl=2&rn=20&tn=news&word=a">体 育(好啊,加油!)</a>
<a href="http://news.aa.com/ns?cl=2&rn=20&tn=news&word=a">新闻(fdsafd!$&*#&^&@s)</a>