正则表达式如何提取html标签里面的内容

<p><strong><br>Rufus</strong><br>Dan, Jenny! Over here! </p>
<p><strong>Jenny</strong><br>Hey, dad! </p>
<p><strong>Rufus</strong><br>Hey, hey! You made it. Welcome back! How was your weekend? How was your mom? </p>
像这个里面的 rufus,jenny 。怎么提出来
高分求。急用

只提取rufus,jenny?不行吧。没有规律啊。是把所有的标签内内容提取了吧。

如果是提取标签内的话这么写:
        Pattern pattern = Pattern.compile(">([^<]+)<");
       
 Matcher macher = 
pattern.matcher("<p><strong><br>Rufus</strong><br>Dan,
 Jenny! Over here! 
</p><p><strong>Jenny</strong><br>Hey, dad!
 </p><p><strong>Rufus</strong><br>Hey, 
hey! You made it. Welcome back! How was your weekend? How was your mom? 
</p>");
        
        while (macher.find())
        {
            System.out.println(macher.group(1));
        }

打印结果:
Rufus
Dan, Jenny! Over here! 
Jenny
Hey, dad! 
Rufus
Hey, hey! You made it. Welcome back! How was your weekend? How was your mom?

麻烦采纳我的答案吧,(*^__^*) 嘻嘻……

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-10-13
function getStr(id,str){
var p = document.getElementById(id);
var text = p.innerHTML;
return text.substring(text.indexOf(str),text.indexOf(str)+str.length); 
}
alert(getStr('p1','Rufus'))
//我给第一个p元素加了一个id,是p1,其他的三个也是这样提取出来的。换个id,换个字符就行了。这是不完整的提取字符的方法。如果想较为完整一些,可以在里面加一个判断语句,如果你所搜索的字符不存在,返回一个错误或者警告什么都可以。
//我没有使用正则,根本不需要正则就可以解决了。

第2个回答  2013-09-03

你的标签貌似不太规则吧 <p><strong><br>Rufus</strong><br> 乱嵌呀 

 public void strong()
 {
  int i = 0;
  final String regex = "<strong.*?/strong>";
  final Pattern pt = Pattern.compile(regex);
  final Matcher mt = pt.matcher(ContentArea);
  while (mt.find()) {
   System.out.println(mt.group());
   i++;

   // 获取标题
   final Matcher title = Pattern.compile(">.*?</strong>").matcher(mt.group());
   while (title.find()) {
    System.out.println("strong是:"
      + title.group().replaceAll(">|</strong>", ""));
  }
   System.out.println();
  }

  
  public static void main(String[] args)
 {
  Urls myurl = new Urls("<body", "/body>");
  myurl.getStartUrl("...");//网址
  myurl.getUrlContent();
  myurl.getContentArea();
  myurl.strong();
 }

本回答被网友采纳
第3个回答  2015-03-19
$str="<li><a href='xxx' target=\"_blank\">yyy</a><div class=\"i1\"></div><i>zzz</i></li><li><a href='xxx1' target=\"_blank\">yyy1</a><div class=\"i1\"></div><i>zzz1</i></li>";
$pattern='/<li><a[^>]+href=\'([^\']*)\'[^>]*>([^<]*)<\/a>.*<i>([^<]*)<\/i><\/li>/iUs';
preg_match_all($pattern, $str, $matches);
print_r($matches);

看下可以不,解析出来的数组应该知道怎么解吧!

第4个回答  2017-08-26
思路:先解析html文件,可以用digester等第三方包。
想直接用正则表达式,不建议。
正则用的更多是校验格式,例如邮箱格式等。
相似回答