homelinksBlijf op de hoogte van de recente aanpassingen op vbvoorbeelden!
Visual Basic 2010 Voorbeelden

visual basic 2010 broncode voorbeelden

Microsoft Visual Studio 2010Microsoft Developers Network - Visual BasicMicrosoft .NET Framework

27.2. Regular Expressions - Literal Charachters

Print Email Deel op Facebook Deel op Twitter

Dit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.

Indien men op zoek gaat naar karakters, andere dan . $ ^ { [ ( | ) * + ? \, kan men deze gewoon opnemen in het pattern :
Visual Basic 2010 Broncode
Imports System.Text.RegularExpressions
Class Example1
    Public Shared Sub Main()
        Dim input As String = "ab'-,a"
        Example1.Print(input, "a")  ' Any character a.
        Example1.Print(input, "'")  ' Any single quote.
        Example1.Print(input, "-")  ' Any dash.
        Example1.Print(input, ",")  ' Any comma.
        Example1.Print(input, "ab") ' Any string ab.
        '
        Console.ReadLine()
    End Sub
    Public Shared Sub Print(ByVal input As String, ByVal pattern As String)
        Console.Write("""" & input & """ has")
        Dim r As Regex = New Regex(pattern)
        Dim m As Match = r.Match(input)
        If m.Success Then
            Do
                Console.Write(" match """ & m.Value & """ " & _
                              "at index " & m.Index.ToString())
                m = m.NextMatch()
            Loop While m.Success
        Else
            Console.Write(" no match")
        End If
        Console.WriteLine(" for pattern """ & pattern & """")
    End Sub
End Class
Console Application Output
"ab'-,a" has match "a" at index 0 match "a" at index 5 for pattern "a"
"ab'-,a" has match "'" at index 2 for pattern "'"
"ab'-,a" has match "-" at index 3 for pattern "-"
"ab'-,a" has match "," at index 4 for pattern ","
"ab'-,a" has match "ab" at index 0 for pattern "ab"

27.2.1. Escaped Karakters

Bepaalde karakters worden in patterns gebruikt voor specifieke doeleinden, deze karakters zijn : . $ ^ { [ ( | ) * + ? \.

Ook als we opzoek gaan naar deze speciale karakters moeten we deze in onze patterns laten voorafgaan door een backslash :
Visual Basic 2010 Broncode
Imports System.Text.RegularExpressions
Class Example2
    Public Shared Sub Main()
        Dim input As String = ".$^{[(|)*+?\"
        Example1.Print(input, "\.") ' Any point.
        Example1.Print(input, "\$") ' Any dollar sign.
        Example1.Print(input, "\^") ' Any caret.
        Example1.Print(input, "\{") ' Any opening braces.
        Example1.Print(input, "\[") ' Any opening square bracket.
        Example1.Print(input, "\(") ' Any opening parenthesis.
        Example1.Print(input, "\|") ' Any vertical bar.
        Example1.Print(input, "\)") ' Any closing parenthesis.
        Example1.Print(input, "\*") ' Any asterix.
        Example1.Print(input, "\+") ' Any addition sign.
        Example1.Print(input, "\?") ' Any questionmark.
        Example1.Print(input, "\\") ' Any backslash.
        '
        Console.ReadLine()
    End Sub
End Class
Console Application Output
".$^{[(|)*+?\" has match "." at index 0 for pattern "\."
".$^{[(|)*+?\" has match "$" at index 1 for pattern "\$"
".$^{[(|)*+?\" has match "^" at index 2 for pattern "\^"
".$^{[(|)*+?\" has match "{" at index 3 for pattern "\{"
".$^{[(|)*+?\" has match "[" at index 4 for pattern "\["
".$^{[(|)*+?\" has match "(" at index 5 for pattern "\("
".$^{[(|)*+?\" has match "|" at index 6 for pattern "\|"
".$^{[(|)*+?\" has match ")" at index 7 for pattern "\)"
".$^{[(|)*+?\" has match "*" at index 8 for pattern "\*"
".$^{[(|)*+?\" has match "+" at index 9 for pattern "\+"
".$^{[(|)*+?\" has match "?" at index 10 for pattern "\?"
".$^{[(|)*+?\" has match "\" at index 11 for pattern "\\"

Dit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.