0

我试图找出以下的正则表达式,但似乎无法正确。有人可以给我建议吗?

简而言之,我有一个 htmlString,它是:

        htmlString = "<HTML><HEAD></HEAD><BODY>Here are some images.</br>1) <IMG style='MARGIN-BOTTOM: 20px; MARGIN-LEFT: 20px' align=right src='images/sample001.jpg'>2) <IMG style='MARGIN-BOTTOM: 25px; MARGIN-LEFT: 25px' align=right src='images/sample002.png'></br> And some docs as well.</br>1) href='javascript:parent.POPUP({url:'testDoc001.htm',type:'shared',width:600,height:645})'></br>2) href='javascript:parent.POPUP({url:'testDoc002.html',type:'shared',width:700,height:712})'></br></BODY></HTML>";

我在 C#,WPF 中通过以下例程运行它:


    private static List<string> ExtractData(string htmlString)
    {
        List<string> data = new List<string>();

        //***  Get The Images ***
        string pattern = @"<img .* src='(.+\.(jpg|bmp|png))'";

        Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
        MatchCollection matches = rgx.Matches(htmlString);

        for (int i = 0, l = matches.Count; i < l; i++)
        {
            data.Add(matches[i].Value);
        }

        //***  Get Html Pages ***
        pattern = @"url:'([^']*)'";

        rgx = new Regex(pattern, RegexOptions.IgnoreCase);
        matches = rgx.Matches(htmlString);

        for (int i = 0, l = matches.Count; i < l; i++)
        {
            data.Add(matches[i].Value);
        }

        return data;
    }--------------------------------------------------------------------------------------

我得到的结果是:

[0] = "< IMG style='MARGIN-BOTTOM: 20px; MARGIN-LEFT: 20px' align=right src='images/sample001.jpg'>2) < IMG style='MARGIN-BOTTOM: 25px; MARGIN-左:25px' align=right src='images/sample002.png'"

[1] = "url:'testDoc001.htm'"

[2] = "url:'testDoc002.html'"

我真正想要的是:

[0] =“图像/sample001.jpg”

[1] = "图像/sample002.png"

[2] = "testDoc001.htm"

[3] = "testDoc002.html"

有人可以告诉我我在正则表达式中做错了什么吗?

谢谢

4

1 回答 1

1

您最好使用HTML Agility Pack进行此类工作。正如其他人所提到的,用于解析 HTML 的正则表达式,除了非常具体的情况外,是一件很糟糕的事情。无论如何,您的正则表达式存在几个问题。第一个应该类似于:

<img.+?src\s*=\s*\'(.*?\.(jpg|bmp|png))'
于 2012-04-16T14:07:33.347 回答