关于正则表达式多次匹配的问题

有一个字符串里头有许多这种格式的a标签<a href="http://qd.anjuke.com" class="">青岛</a>当然内容是不同的,尝试了这个正则表达式:<a\s+href=\"(.+)\"\s+class=\"\">(.+).*</a> 获取到了 网址和城市名称,但是只能获取到第一个a标签中的内容,请问想要获取所有a标签里的网址和城市名称改怎么做?
对了,我用的是C#,如果有办法的话请将代码详细贴出

正则表达式:<a[\s\S]+?href=\"([\s\S]*?)\"[\s\S]*?>([\s\S]*?)</a>

C#正则表达式:<a[\\s\\S]+?href=\"([\\s\\S]*?)\"[\\s\\S]*?>([\\s\\S]*?)</a>

因为字符串中含有大量超链接,而百度知道一般不允许发布超链接,所以我给你个C#的小例子:

using System;

using System.Collections.Generic;

using System.Text;

using System.Text.RegularExpressions;

namespace ConsoleApplication3

{

    class Program

    {

        static void Main(string[] args)

        {

              string s = "<a log=\"sc_pos:c_news\" rel=\"nofollow\" href=\"xxxxxx\">新闻</a><a href=\"xxxxx\" class=\"\">青岛</a>";

            Regex r = new Regex("<a[\\s\\S]+?href=\"([\\s\\S]*?)\"[\\s\\S]*?>([\\s\\S]*?)</a>");

            MatchCollection matches = r.Matches(s);

            for (int i = 0; i < matches.Count; i++)

            {

                if (matches[i].Success)

                {

                    Console.Write(matches[i].Groups[1].Value+" ");

                    Console.WriteLine(matches[i].Groups[2].Value);

                }

            }

            Console.ReadKey();

        }

    }

}

运行结果:

xxxxxx 新闻

xxxxx 青岛

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答