How can I convert the list of match result from regex into List<string>
? I have this function but it always generate an exception,
Unable to cast object of type 'System.Text.RegularExpressions.Match'
to type 'System.Text.RegularExpressions.CaptureCollection'.
public static List<string> ExtractMatch(string content, string pattern)
{
List<string> _returnValue = new List<string>();
Match _matchList = Regex.Match(content, pattern);
while (_matchList.Success)
{
foreach (Group _group in _matchList.Groups)
{
foreach (CaptureCollection _captures in _group.Captures) // error
{
foreach (Capture _cap in _captures)
{
_returnValue.Add(_cap.ToString());
}
}
}
}
return _returnValue;
}
If I have this string,
I have a dog and a cat.
regex
dog|cat
I want that the function will return of result into List<string>
dog
cat
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…