TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Regex.Matches Method: foreach Match, Capture

Use the Regex.Matches method to extract substrings based on patterns.
Regex.Matches returns multiple Match objects. It matches multiple instances of a pattern and returns a MatchCollection. It is useful for extracting values, based on a pattern, when many are expected.Regex
To start, this program includes the System.Text.RegularExpressions namespace. It uses an input string that contains several words. Each one starts with the letter s. Next, we use Regex.Matches on this string.

Pattern: This will only match words starting with the letter s, with one or more non-whitespace characters, and ending in a lowercase d.

Foreach: We use foreach on the MatchCollection, and then on the result of the Captures property.

Foreach

Tip: To access the individual pieces matched by the Regex, we need to loop over the Captures collection.

Finally: We can access the Value property to get the actual string. The Index property tells us the character position of the capture.

C# program that uses Regex.Matches method using System; using System.Text.RegularExpressions; class Program { static void Main() { // Input string. const string value = @"said shed see spear spread super"; // Get a collection of matches. MatchCollection matches = Regex.Matches(value, @"s\w+d"); // Use foreach-loop. foreach (Match match in matches) { foreach (Capture capture in match.Captures) { Console.WriteLine("Index={0}, Value={1}", capture.Index, capture.Value); } } } } Output Index=0, Value=said Index=5, Value=shed Index=20, Value=spread
Matches, Match. With Regex.Matches, we gather a group of matches (a MatchCollection) which must be iterated over. If only one Match is needed, Regex.Match is simpler to use. And the simplest method is typically best.
Summary. The Regex.Matches method provides a way for you to match multiple times in a single input string. You can then loop over these matches and their individual captures to get all the results. It is similar to Regex.Match.

Note: Doing this sort of text processing would be more cumbersome if you were to use methods such as IndexOf and Split.

IndexOfSplit
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf