Visual Basic 2012 Voorbeelden
   

visual basic 2012 broncode voorbeelden

Blijf op de hoogte van de recente aanpassingen op vbvoorbeelden!

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

28.2. Regular Expressions - Literal Charachters

Print Email Deel op Twitter Deel op Facebook

Dit artikel is gepubliceerd op maandag 15 oktober 2012 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 2012 Broncode - Codevoorbeeld 700
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"

28.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 2012 Broncode - Codevoorbeeld 701
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 maandag 15 oktober 2012 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.