这个正则表达式怎么写?

shibai 2008-04-20 03:21:23
比如有下面一段代码:
<a href="11"><font color="21">aaa</font></a>
<a href="12"><font color="22">bbb</font></a>
<a href="13">ccc</a>
<a href="14"><font color="24">ddd</font></a>
<a href="15"><font color="25">eee</font></a>
<a href="16">fff</a>

上面的代码意思是<font color="***">和</font>不一定有,而且color的值也可能不一样
我现在想得到
aaa
bbb
ccc
ddd
eee
fff

这个正则表达式该怎么写?谢谢
...全文
134 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ORACLE800 2008-04-20
  • 打赏
  • 举报
回复
7楼的使用了非捕获组,学习了。
ORACLE800 2008-04-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 shibai 的回复:]
引用 5 楼 zhuche110 的回复:
楼主运行下偶的代码 ,行的话别忘了给分^_^


能不能一个正则表达式搞定不用后边的replaceAll阿
[/Quote]

只是提供一种思路。
7楼的方法不错,可以实现。
  • 打赏
  • 举报
回复
import java.util.Pattern;
import java.util.Matcher;

public class Test {
public static void main(String[] args) {
String str = "<a href=\"11\"> <font color=\"21\">aaa </font> </a>" +
"<a href=\"12\"> <font color=\"22\">bbb </font> </a>" +
"<a href=\"13\">ccc </a> " +
"<a href=\"14\"> <font color=\"23\">ddd </font> </a>" +
"<a href=\"15\"> <font color=\"25\">eee </font> </a> " +
"<a href=\"16\">fff </a> ";
String regex = "<a[^>]*>(?:\\s*<font[^>]*>)?(.*?)(?:</font>\\s*)?</a>";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while(matcher.find()) {
System.out.println(matcher.group(1));
}
}
}
shibai 2008-04-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zhuche110 的回复:]
楼主运行下偶的代码 ,行的话别忘了给分^_^
[/Quote]

能不能一个正则表达式搞定不用后边的replaceAll阿
ORACLE800 2008-04-20
  • 打赏
  • 举报
回复
楼主运行下偶的代码 ,行的话别忘了给分^_^
ORACLE800 2008-04-20
  • 打赏
  • 举报
回复
package test1;
import java.util.regex.*;
public class Test6
{
public static void main(String[] args)
{
String s="<a href=\"11\"> <font color=\"21\">aaa </font> </a> "
+"<a href=\"12\"> <font color=\"22\">bbb </font> </a> "
+"<a href=\"13\">ccc </a> "
+"<a href=\"14\"> <font color=\"23\">ddd </font> </a>"
+"<a href=\"15\"> <font color=\"25\">eee </font> </a> "
+"<a href=\"16\">fff </a> ";
String regex="<a.*?>(.*?)</a>";

Pattern pt=Pattern.compile(regex);
Matcher mt=pt.matcher(s);
while(mt.find())
{
System.out.println(mt.group(1).replaceAll("<font.*?>|</font>", "").trim());
}
}
}
carvin_happy 2008-04-20
  • 打赏
  • 举报
回复
问一下楼主,你是想用js还是java啊?
anqini 2008-04-20
  • 打赏
  • 举报
回复

/**
* kimi
*/

package test;

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

/**
* @author kimi
*
*/
public class TestCsdn {

/*
* <a href="11"> <font color="21">aaa </font> </a> <a href="12"> <font color="22">bbb </font> </a> <a href="13">ccc
* </a> <a href="14"> <font color="24">ddd </font> </a> <a href="15"> <font color="25">eee </font> </a> <a
* href="16">fff </a>
*
*/
/**
* @param args
*/
public static void main(String[] args) {
String s1 = "<a href=\"11\"> <font color=\"21\">aaa </font> </a>\n"
+ "<a href=\"12\"> <font color=\"22\">bbb </font> </a>\n" + "<a href=\"13\">ccc </a>";
String s = "(<a href=\"\\d{2}\">(<font color=\"\\d{2}\">(.*?)</font>)?|(.*?)</a>[\n\r]?)+";
Pattern p = Pattern.compile(s);
Matcher m = p.matcher(s1);
if (m.matches()) {
System.out.println("ok");
}
}
}


初学EL,请多多关照!
  • 打赏
  • 举报
回复
用捕获组
public static void main(String args[]) {
String str="<a href='13'>ccc </a> ";
Pattern p=Pattern.compile(">([\\w ]+)<");
Matcher m=p.matcher(str);
while(m.find())
System.out.print(m.group(1));
}
至于你那个aaa bbb ccc我想实际上应该是一个字符串吧 你自己改吧 把">([\\w ]+)<"括号里的表达式按需要改吧 然后m.group(1)就是你要的 应该可以的 而且后面还一个空格 倒

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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