VB regex

VB (.NET) provides a Regex class that can be used to work with regular expressions. It is commonly used to search for a complex string pattern in another string or to replace occurences of a sub-string within a string that matches the pattern.


-Search for a complex string pattern: Regex.Match(string, pattern)
+string contains a sub-string that matches the string pattern.
+pattern is the expression to be matched.


When writing a pattern the following special characters are useful:
\d: matches any digit character from 0 to 9.
\D: matches any character that is not a digit.
\s: matches any character that is a whitespace including tab, carriage return, form feed, new line, and vertical tab.
\S: matches any character that is not a whitespace character.
\w: matches any character that is a character from a to z, A to Z, and underscore.
\W: matches any character that is a not a character from a to z, A to Z, 0 to 9, and underscore.
[...]: matches any character that is in  brackets.
[^...]: matches any character that is not in brackets.
.: matches any character that is not a new line.
?: matches the previous item 0 time or 1 time.
*: matches the previous item 0 time or more times.
+: matches the previous item 1 time or more times.
{n}: matches the previous item n times.
{n,}: matches the previous item more than n times.
{n,m}: matches the previous item from n to m times.
^: matches a pattern at the beginning of a string.
$: matches a pattern at the end of a string.

Example: To search for the first whitespace or the first group of whitespaces in the string "Regex provides power to programmers.", the pattern should be "\s+".

Imports System.Text.RegularExpressions
Module Module1

   Sub Main()
     Dim string1 As String = "Regex provides power to programmers."
     Dim pattern As String = "\s+"
     Dim mat = Regex.Match(string1, pattern)
     If mat.Success Then
        Console.WriteLine("Found at position " & mat.Index)
     Else
        Console.WriteLine("No found")
     End If
  End Sub
End Module

If you wan to search for all whitespaces, use Matches(string,pattern) method instead of Match(string,pattern) method.

Imports System.Text.RegularExpressions
Module Module1

   Sub Main()
     Dim string1 As String = "Regex   provides     power to programmers."
     Dim pattern As String = "\s+"
     Dim mat = Regex.Matches(string1, pattern)
     If mat.Count > 0 Then
        For Each x In mat
           Console.WriteLine("Index:" & x.index)

        Next
     Else
        Console.WriteLine("No match found!")
     End If
     Console.ReadLine()

   End Sub
End Module

If you want to show all matched values, use the Value property of the match result object.

Imports System.Text.RegularExpressions
Module Module1

   Sub Main()
     Dim string1 As String = "My number is 9999. Your number is 89"
     Dim pattern As String = "\d+"
     Dim x As Match

     Dim mat = Regex.Matches(string1, pattern)
     If mat.Count > 0 Then
        For Each x In mat
           Console.WriteLine("Found " & x.Value & " at index:" & x.Index)

        Next
     Else
        Console.WriteLine("No match found!")
     End If
     Console.ReadLine()

   End Sub
End Module

-Replace occurrences of a sub-string: Reg.Repalce(string,pattern,replacewith)

Example: replace all groups of digits with hyphen

Imports System.Text.RegularExpressions
Module Module1

   Sub Main()
     Dim string1 As String = "My number is 9999. Your number is 89."
     Dim pattern As String = "(\d+)"
     Dim strresult = Regex.Replace(string1, pattern, "-")
     Console.WriteLine(strresult)
     Console.ReadLine()

   End Sub
End Module

If you wan to match or replace different sub-strings, you will use the | sign to separate the patterns. The following example, replace all groups of digits, whitespaces, and symbols in "My number is 9999. Your number is 89" string with hyphen.

Imports System.Text.RegularExpressions
Module Module1

   Sub Main()
     Dim string1 As String = "My money is 10000$. Your money is only is 100$."
     Dim pattern As String = "(\d+|\s+|\W+)"
     Dim strresult = Regex.Replace(string1, pattern, "-")
     Console.WriteLine(strresult)
     Console.ReadLine()

   End Sub
End Module

Posted by: Dara | post date: 02-15-2013 | Subject: VB.NET Programming Language




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.