2017-05-02 2 views
0

私は文字列を持っています[文字列のGUIDパターンだけではなく、HtmlAgilityPackを使用してhtmlnodesに解析して変換しています。ノードが含まれ、抽出IDおよびタイプ= \「ClickButton \」値= 'アップロード、簡単にするために私はすべての詳細を縮小]特別な複数文字列分割C#

"\r\n      <extractable id=\"00000000-0000-0000-0000-000000000000\" class=\"myButtonC\" type=\"ClickButton\" value='upload'>\r\n     " 

私はそれからGUIDを抽出したいです。これはHTML解析の一部です。だから私は以下の方法を使用して抽出を試み、動作していないようです。 "\" "と" = \ ""をどのように表しますか?私はリテラルに対して "as \"と\ as \を使用しました。なにか提案を?

private static string ExtractId(string str)  
{ 
    string eId = string.Empty; 
    string[] arrys = str.Split(new string[] {@"\"" "}, StringSplitOptions.None); 
    foreach (string[] lists in arrys.Select(t => t.Split(new string[] {@"=\"""}, StringSplitOptions.None))) 
    { 
     for (int j = 0; j < lists.Length; j++) 
     { 
      if (lists[j].Contains("extractable id")) 
      { 
       eId = lists[j + 1]; 
      } 
     } 
    } 
    return eId; 
} 
+0

可能な複製](http://stackoverflow.com/questions/29138593/regular-expression-to-identify-a-guid-followed-by-a-number) –

+0

そのようなxml try t o xmlリーダーを使用する –

+0

...または[C#Regex for Guid](http://stackoverflow.com/a/11040993/205233) - ちょうどGUIDを探して、物事をより簡単にします。 – Filburt

答えて

0

どのように正規表現

string pattern = @"([a-z0-9]{8}[-][a-z0-9]{4}[-][a-z0-9]{4}[-][a-z0-9]{4}[-][a-z0-9]{12})"; 

MatchCollection mc = Regex.Matches(your_string, pattern); 

foreach (var sGUID in mc) 
{ 
    // do what you want 
} 
3

私はGuid Sに一致するように正規表現を使用することをお勧めの使用方法について:

string source = "\r\n <extractable id=\"00000000-0000-0000-0000-000000000000\" class=\"myButtonC\" type=\"ClickButton\" value='upload'>\r\n"; 

Guid[] result = Regex 
    .Matches(
    source, 
    "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}") 
    .OfType<Match>() 
    .Select(match => new Guid(match.Value)) 
    .ToArray(); 
数字が続くGUIDを識別するために、[正規表現の
関連する問題