java 正则表达式获取匹配

比如有下面一段代码:
<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

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());
        }
    } 
} 

 

使用非捕获组

 

package test1;
import java.util.regex.*;
 
public class Test6 ...{
    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));
        }
    }
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值