Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert result of matches from regex into list of string

Tags:

c#

.net

regex

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 
like image 654
SkyDrive Avatar asked Oct 04 '12 15:10

SkyDrive


People also ask

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

How do you check if a regex matches a string?

Use the test() method to check if a regular expression matches an entire string, e.g. /^hello$/. test(str) . The caret ^ and dollar sign $ match the beginning and end of the string. The test method returns true if the regex matches the entire string, and false otherwise.

What does match return in regex?

The Match(String, String) method returns the first substring that matches a regular expression pattern in an input string. For information about the language elements used to build a regular expression pattern, see Regular Expression Language - Quick Reference.

What is D * in regex?

\d (digit) matches any single digit (same as [0-9] ). The uppercase counterpart \D (non-digit) matches any single character that is not a digit (same as [^0-9] ).


2 Answers

With the Regex you have, you need to use Regex.Matches to get the final list of strings like you want:

MatchCollection matchList = Regex.Matches(Content, Pattern); var list = matchList.Cast<Match>().Select(match => match.Value).ToList(); 
like image 62
manojlds Avatar answered Oct 02 '22 07:10

manojlds


To get just a list of Regex matches, you may:

var lookfor = @"something (with) multiple (pattern) (groups)"; var found = Regex.Matches(source, lookfor, regexoptions); var captured = found                 // linq-ify into list                 .Cast<Match>()                 // flatten to single list                 .SelectMany(o =>                     // linq-ify                     o.Groups.Cast<Capture>()                         // don't need the pattern                         .Skip(1)                         // select what you wanted                         .Select(c => c.Value)); 

This will "flatten" all the captured values down to a single list. To maintain capture groups, use Select rather than SelectMany to get a list of lists.

like image 27
drzaus Avatar answered Oct 02 '22 08:10

drzaus