Convert regex matches to the list of strings

c# regex extract string
regex match examples
regex match on list python
c# regex match exact string
c# regex to match any string
c# regex test
c# regex multiple matches
convert string to regex c#

I'm trying to find equal sub-string in big list about 50 000 strings, this way fine:

var results = myList.FindAll(delegate (string s) { return s.Contains(myString); });

but it also looks for sub-string with part of word, for example, if I'm looking for "you do" it founds also extra "you dont" because contains "you do..".

So, this answer to my previous question supposedly should work as i need, but I'm not sure, how to get strings list from regex matches for particular code:

foreach (string phrase in matchWordsList)
     foreach (string str in bigList)
          string[] stringsToTest = new[] { phrase };
          var escapedStrings = stringsToTest.Select(s => Regex.Escape(s)); 
          var regex = new Regex("\\b(" + string.Join("|", escapedStrings) + ")\\b");
          var matches = regex.Matches(str);

          foreach (string result in matches) /// Incorrect: System.InvalidCastException 

Getting strings from matches directly to the list throws exception:

An unhandled exception of type 'System.InvalidCastException' occurred in test.exe

Additional information: Unable to cast object of type 'System.Text.RegularExpressions.Match' to type 'System.String'.

So, I'm trying to figure out, hot to convert var matches = regex.Matches(str); to the list

You can do it with linq. However you will need to Cast it first then Select

var resultsList = regex.Matches(str)
                       .Select(m => m.Value)


         .Select(m => m.Value));

Convert regex matches to the list of strings, I'm trying to find equal sub-string in big list about 50 000 strings, this way fine: You can try to convert your result into an array and apply the string.Join to put your string in flat here you must specify the Match type explicitly as the MatchCollection is a non-generic IEnumerable type . var toarray = from Match match in matchCollection select match.Value; string newflatChain = string.Join(";", toarray);

Simply use Match type in foreach loop:

foreach (Match result in matches)

Matches to List<String> - Build, I have a Match activity that outputs an IEnumerable. Would like to Converting a MatchCollection to string array. c#, arrays, regex. asked by Vil  This tool extracts substring fragments of a string that match the given regular expression. Make sure to specify the 'g' flag, if you want to get a list of all matches. In the options above, you can specify the regular expression and customize the delimiter character for output matches. Stringabulous!

I may have misunderstood what you were trying to do in your previous question.

Would this work? It combines your "matchWordsList" into a single expression, and then adds each match from bigList into resultsList:

var escapedStrings = matchWordsList.Select(s => Regex.Escape(s)); 
var regex = new Regex("\\b(" + string.Join("|", escapedStrings) + ")\\b");
foreach (string str in bigList)
    if (regex.IsMatch(str))

So if matchWordsList contains ["test","words","cheese"], and str is "This is a test to check if Regex is matching words. I like cheese.", it will add str to resultsList once (even though there are 3 matches).

Try it online

How to convert matched Regex patterns to String? - Build, Searches an input string for a substring that matches a regular expression finds regular expression pattern matches in a string, then lists the matched groups,  The regular expression engine is responsible for parsing and compiling a regular expression, and for performing operations that match the regular expression pattern with an input string. The regex module releases the GIL during matching on instances of the built-in (immutable) string classes, enabling other Python threads to run concurrently.

Regex.Match Method (System.Text.RegularExpressions), To top it off, the full stop at the end of the string is the final match. RegexBuddy provides this option to make it straightforward to specify multiple test line breaks”, which converts line breaks to fit the regex engine of the selected application  Regex not working in HTML5 pattern. regex,html5. The pattern attribute has to match the entire string. Assertions check for a match, but do not count towards the total match length. Changing the second assertion to \w+ will make the pattern match the entire string. You can also skip the implied ^, leaving you with just: <input pattern="(?!34

Full Documentation to The World's Most Comprehensive Regex Editor, 25.3 List-columns · 25.4 Creating list-columns Regular expressions are useful because strings usually contain unstructured or You need to use an “escape” to tell the regular expression you want to match it exactly, not use its special behaviour. Rmd , you can convert them to regular expressions with glob2rx() ):​  web developer and programmer tools World's simplest string from regexp generator. Just enter your regex in the field below, press Generate Text button, and you get random data that matches your regular expression. Press button, get regex matching strings.

14 Strings, A regular expression (or RE) specifies a set of strings that matches it; the Now we convert the string into a list with each nonempty line having its own entry:. I have a list of strings in which I want to filter for strings that contains keywords. I want to do something like: fruit = re.compile('apple', 'banana', 'peach', 'plum', 'pinepple', 'kiwi'] so I can then use, list_of_strings) to get only the strings containing fruits, but I'm not sure how to use a list with re.compile.

  • What should the list contain?
  • @John resultsList must add result of matches from each string of bigList for each phrase by matchWordsList
  • This is wrong : foreach (string result in matches). Matches returns a MatchCollection not a string. I always tell people only use var when necessary. You should of used : MatchCollection matches = regex.Matches(input, pattern); Then the compiler would of found the error instead of getting a run-time error.
  • Well, this looks like answer to my previous question, but in this topic I have to mark another one according to particular question. Can you edit your answer with this code in previous post to make me able to mark it as answer, please
  • I kind of think my answer to your previous question is correct. You wanted the index of matches in your previous question. In this question, you just seem to want to record a list of strings that match one of the values in "matchWordsList", hence my different answer. Is that not correct?