TheDeveloperBlog.com

Home | Contact Us

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

<< Back to VBNET

VB.NET Regex.Matches: For Each Match, Capture

Use Regex.Matches to match Strings based on patterns. Loop with For Each on Matches and Captures.
Regex.Matches uses a pattern argument. It searches the source String to find all matches to that pattern. It then returns those matches in Match objects as part of a MatchCollection. Regex.Matches is useful for complex text processing.Regex.MatchStrings
To start, this program imports the System.Text.RegularExpressions namespace. In the Main subroutine, we use the Regex.Matches method on a String literal containing several words starting with "s".

And: The pattern in the Regex specifies that a match must start with s, have one or more non-whitespace characters, and end in d.

Info: The Regex.Matches function returns a MatchCollection. We use the For-Each looping construct on it (matches).

For Each, For

Then: In a nested loop, we enumerate all the Capture instances. We access the Index and Value from each Capture.

Note: Each capture has an Index (the character position of the capture in the source string) and a Value (the matching substring itself).

VB.NET program that uses Regex.Matches Imports System.Text.RegularExpressions Module Module1 Sub Main() ' Input string. Dim value As String = "said shed see spear spread super" ' Call Regex.Matches method. Dim matches As MatchCollection = Regex.Matches(value, "s\w+d") ' Loop over matches. For Each m As Match In matches ' Loop over captures. For Each c As Capture In m.Captures ' Display. Console.WriteLine("Index={0}, Value={1}", c.Index, c.Value) Next Next End Sub End Module Output Index=0, Value=said Index=5, Value=shed Index=20, Value=spread
Regex.Match. The Regex.Matches function is basically the same as the Regex.Match function except that it returns all matches rather than just the first one. If you need to find more than just one match, Regex.Matches is the best function to choose.
Summary. With Regex.Matches, you can find all the matching parts in a source string with a regular expression pattern. You need to then loop over all the individual matches. When there may be many matching parts, Regex.Matches is necessary.
© 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