[C#]規則運算式(Regular Expression)解析字串

  • 7379
  • 0

[C#]規則運算式(Regular Expression)解析字串

假設目前得到字串為

要取得A("")的內容


  
// Instantiate the regular expression object.   
Regex reg = new Regex("A(\".*?\")");   
  
// Match the regular expression pattern against a text string.   
Match match = reg.Match(str.Replace("(", "").Replace(")", ""));   
  
while (match.Success)   
{   
    CaptureCollection captures = match.Captures;   
  
    for (int i = 0; i < captures.Count; i++)   
    {   
        parameterValues.Add(captures[i].Value.Replace("A", string.Empty).Replace("\"", ""));   
    }   
  
    match = match.NextMatch();   
}

 

這樣就可以取得1和3的值…

 

參考來源:http://msdn.microsoft.com/zh-tw/library/twcw2f1c.aspx

新手發文請多多指教 ^_____^