2013-10-21 40 views
1

剛剛新到C#..我需要一些幫助..即時嘗試將此轉換爲C#,但不知道什麼是相當於preg_match_all在C#..一直在讀一些書,但我沒有得到它..:'(C#版本的preg_match

$pattern = 
    '@' . 
    '<td>\s*' . 
    '(?P<no>\d+)\.\s*' . 
    '</td>\s*' . 
    '<td>\s*' . 
    '<a class="LN" href="[^"]*+" onclick="[^"]*+">\s*+' . 
    '<b>(?P<name>[^<]*+)</b>\s*+' . 
    '</a>.*\s*' . 
    '</td>\s*+' . 
    '<td align="center">[^<]*+</td>\s*+' . 
    '<td>\s*+' . 
    '(?P<locations>(?:<a href="[^"]*+">[^<]*+</a><br />\s*+)++)' . 
    '</td>' . 
    '@' 
    ; 

    $results = array(); 
    preg_match_all($pattern, $contents, $matches, PREG_SET_ORDER); 
    foreach ($matches as $i => $match) { 
     preg_match_all('@<a href="[^"]*+">([^<]*+)</a>@', $match['locations'], $locations); 
     $results[$i]['no'] = $match['no']; 
     $results[$i]['name'] = $match['name']; 
     $results[$i]['locations'] = $locations[1]; 
    }** 
+3

它看起來像它的使用正則表達式來嘗試分析一些HTML - 這將是更好地使用像一個合適的工具[HTML敏捷包](http://htmlagilitypack.codeplex.com/)。 –

+0

WebClient怎麼樣? – Cindy93

回答

1

你必須這樣寫:

foreach (Match match in Regex.Matches(contents, pattern, RegexOptions.IgnoreCase)) 
{ 
    string no = match.Groups["no"].Value; 
    ///...etc 
} 
+0

...爲什麼我得到無法識別的轉義序列? – Cindy93

+0

我認爲是因爲你錯誤地使用了反斜槓。例如:'string path = @「C:\ Windows \ system32」;'它是模擬的'string path =「C:\\ Windows \\ system32」;' – progpow

+0

謝謝..現在我收到一個錯誤無法識別的分組結構。 – Cindy93

1

使用靜態方法

public static Match Match(
    string input, 
    string pattern, 
    RegexOptions options 
) 

這個函數返回:

System.Text.RegularExpressions.Match一個對象,它包含 關於比賽的信息。

欲瞭解更多信息請參閱Here