2010-03-15 26 views
1

我試圖捕獲正在重複的HTML頁面中的網址,並且它通常在網址位於不同行時起作用,但在這種情況下,它們全部顯示在一行和單獨的行中。 URL具有標籤: HTTP://東西/ profile文件「> 這是我一直在努力使用正則表達式匹配重複組

Dim regex As Regex = New Regex(_ 
          ".*<a.*href='http://(?<Link>.*?)/profile'>", _ 
          RegexOptions.IgnoreCase _ 
          Or RegexOptions.CultureInvariant _ 
          Or RegexOptions.IgnorePatternWhitespace _ 
          Or RegexOptions.Compiled _ 
          ) 


      Dim ms As MatchCollection = regex.Matches(_html) 
      Dim url As String = String.Empty 
      For Each m As Match In ms 
       url = m.Groups("Link").Value.ToLower 

讚賞任何想法。

回答

1

您可能需要添加RegexOptions.SingleLine。從文檔:

指定單線模式。 點的含義(。)所以它 匹配每個字符(而不是除\ n之外的每個字符的 )。

2

當存在稱爲HTML Agility Pack的奇妙庫時,不需要使用Regex來嘗試解析HTML。這個庫很容易找到鏈接,它會正確處理你的正則表達式會失敗的特殊情況。您將獲得一個更強大的解決方案,而且涉及更少的工作。

這表明使用該庫的示例代碼是用C#,但希望它會幫助你在VB.NET建立一個解決方案:

HtmlDocument doc = new HtmlDocument(); 
doc.Load("input.html"); 
foreach (var link in doc.DocumentNode.Descendants("a")) 
{ 
    string href = link.Attributes["href"].Value; 
    Match match = Regex.Match(href, "^http://(?<Link>.*?)/profile$"); 
    if (match.Success) 
    { 
     Console.WriteLine(match.Groups["Link"].Value); 
    } 
} 
+0

非常感謝您的回覆,我會考慮在未來的程序中應用此代替正則表達式 – vbNewbie 2010-03-16 14:24:01