VB.NET Regex.Matches Quote ExampleUse the Regex.Matches Function on quoted String data. Ensure quotes are handled.
Regex.Matches can handle quoted data. We parse a String containing fields inside quotes in VB.NET. Special care must be taken with the expression pattern. We show an example of parsing a textual list.Regex.MatchRegex.Matches
To begin, the RegularExpressions namespace must be used in an Imports directive. Next, we call Regex.Matches with a pattern that means we want zero or more characters inside single quotes. The inner part is reluctantly continued.
Also: The question mark specifies that the match should be as short as possible. This means two entries will never be matched as one.
VB.NET program that uses Regex.Matches with quotes
' The input string.
Dim value As String = "('BH','BAHRAIN','Bahrain','BHR','048')"
' Match data between single quotes hesitantly.
Dim col As MatchCollection = Regex.Matches(value, "'(.*?)'")
' Loop through Matches.
For Each m As Match In col
' Access first Group and its value.
Dim g As Group = m.Groups(1)
definitely alternative methods you can use to accomplish this same task. A simple for-loop that appends the substrings to a List would probably be fastest. The Regex.Split method could be called.Regex.SplitFor Each, For
Note: You would have to specify all non-matching characters such as the parentheses, quotes, and commas.
Summary. We extracted substrings from inside certain characters such as quotes. This approach is not the fastest. But it is fairly simple and straightforward. The Regex pattern can be changed as your requirements change as well.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to firstname.lastname@example.org.