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.4. Regular Expressions - Character Classes

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.

28.4.1. Positive, Negative and Ranged Character Groups

Met een "character class", hier ook "character set" genoemd, kan men een reeks van mogelijke karakters opgeven.  Deze reeks wordt tussen vierkante haakjes geplaatst (1).

Indien men een groter bereik wenst op te geven, kan dit door een "character range" te gebruiken, aan de hand van een - symbool (2).

Indien de reeks of bereik van karakters niet mag voorkomen, spreekt men over een "negative character group", hierbij gebruikt men het ^ symbool (3).
Mogen de karakters wel voorkomen, dan heb je een "positive character group".
Visual Basic 2012 Broncode - Codevoorbeeld 703
Imports System.Text.RegularExpressions
Class Example1
    Public Shared Sub Main()
        Dim input As String = "A1Bc23D4ZAB"
        Example1.Print(input, "[ac2]")    ' Any character a, c or 2.         (1)
        Example1.Print(input, "[A-D]")    ' Any character from A to D.       (2)
        Example1.Print(input, "[A-DX-Z]") ' Any character from A to D or X to Z.
        Example1.Print(input, "[^ac2]")   ' Any character except a, c and 2. (3)
        Example1.Print(input, "[^1-3]")   ' Any character except 1, 2 and 3.
        Example1.Print(input, "A[^B]")    ' A text starting with character A
        '                                   followed by a character that is
        '                                   not B.
        '
        Console.ReadLine()
    End Sub
    Public Shared Sub Print(ByVal input As String, ByVal pattern As String)
        Dim r As Regex = New Regex(pattern)
        Dim m As Match = r.Match(input)
        If m.Success Then
            Do
                Console.WriteLine("""" & input & """ has" & _
                                  " match """ & m.Value & """ " & _
                                  "at index " & m.Index.ToString() & _
                                  " for pattern """ & pattern & """")
                m = m.NextMatch()
            Loop While m.Success
        Else
            Console.WriteLine("""" & input & """ has no match" & _
                              " for pattern """ & pattern & """")
        End If
    End Sub
End Class
Console Application Output
"A1Bc23D4ZAB" has match "c" at index 3 for pattern "[ac2]"
"A1Bc23D4ZAB" has match "2" at index 4 for pattern "[ac2]"
"A1Bc23D4ZAB" has match "A" at index 0 for pattern "[A-D]"
"A1Bc23D4ZAB" has match "B" at index 2 for pattern "[A-D]"
"A1Bc23D4ZAB" has match "D" at index 6 for pattern "[A-D]"
"A1Bc23D4ZAB" has match "A" at index 9 for pattern "[A-D]"
"A1Bc23D4ZAB" has match "B" at index 10 for pattern "[A-D]"
"A1Bc23D4ZAB" has match "A" at index 0 for pattern "[A-DX-Z]"
"A1Bc23D4ZAB" has match "B" at index 2 for pattern "[A-DX-Z]"
"A1Bc23D4ZAB" has match "D" at index 6 for pattern "[A-DX-Z]"
"A1Bc23D4ZAB" has match "Z" at index 8 for pattern "[A-DX-Z]"
"A1Bc23D4ZAB" has match "A" at index 9 for pattern "[A-DX-Z]"
"A1Bc23D4ZAB" has match "B" at index 10 for pattern "[A-DX-Z]"
"A1Bc23D4ZAB" has match "A" at index 0 for pattern "[^ac2]"
"A1Bc23D4ZAB" has match "1" at index 1 for pattern "[^ac2]"
"A1Bc23D4ZAB" has match "B" at index 2 for pattern "[^ac2]"
"A1Bc23D4ZAB" has match "3" at index 5 for pattern "[^ac2]"
"A1Bc23D4ZAB" has match "D" at index 6 for pattern "[^ac2]"
"A1Bc23D4ZAB" has match "4" at index 7 for pattern "[^ac2]"
"A1Bc23D4ZAB" has match "Z" at index 8 for pattern "[^ac2]"
"A1Bc23D4ZAB" has match "A" at index 9 for pattern "[^ac2]"
"A1Bc23D4ZAB" has match "B" at index 10 for pattern "[^ac2]"
"A1Bc23D4ZAB" has match "A" at index 0 for pattern "[^1-3]"
"A1Bc23D4ZAB" has match "B" at index 2 for pattern "[^1-3]"
"A1Bc23D4ZAB" has match "c" at index 3 for pattern "[^1-3]"
"A1Bc23D4ZAB" has match "D" at index 6 for pattern "[^1-3]"
"A1Bc23D4ZAB" has match "4" at index 7 for pattern "[^1-3]"
"A1Bc23D4ZAB" has match "Z" at index 8 for pattern "[^1-3]"
"A1Bc23D4ZAB" has match "A" at index 9 for pattern "[^1-3]"
"A1Bc23D4ZAB" has match "B" at index 10 for pattern "[^1-3]"
"A1Bc23D4ZAB" has match "A1" at index 0 for pattern "A[^B]"

28.4.2. Character Class Substraction

De groep van karakters die door een character group worden aangegeven, kunnen we verminderen met een andere character group :
Visual Basic 2012 Broncode - Codevoorbeeld 704
Imports System.Text.RegularExpressions
Class Example2
    Public Shared Sub Main()
        Dim input As String = "abcdehijklmnopquvwxyz"
        Example1.Print(input, "[a-z-[d-w-[k]]]")  ' Any character from a to z,
        '                                           except d to w (except k).
        '
        Console.ReadLine()
    End Sub
End Class
Console Application Output
"abcdehijklmnopquvwxyz" has match "a" at index 0 for pattern "[a-z-[d-w-[k]]]"
"abcdehijklmnopquvwxyz" has match "b" at index 1 for pattern "[a-z-[d-w-[k]]]"
"abcdehijklmnopquvwxyz" has match "c" at index 2 for pattern "[a-z-[d-w-[k]]]"
"abcdehijklmnopquvwxyz" has match "k" at index 8 for pattern "[a-z-[d-w-[k]]]"
"abcdehijklmnopquvwxyz" has match "x" at index 18 for pattern "[a-z-[d-w-[k]]]"
"abcdehijklmnopquvwxyz" has match "y" at index 19 for pattern "[a-z-[d-w-[k]]]"
"abcdehijklmnopquvwxyz" has match "z" at index 20 for pattern "[a-z-[d-w-[k]]]"
Character class substraction is mogelijk vanaf Framework 2.0.

28.4.3. Shorthand Character Classes

Omdat bepaalde character classes vaak gebruikt worden, zijn enkele "shorthand character class" notaties voorzien :

- \d staat voor onder andere [0-9] ("digit character" (decimal digit))
- \w staat voor onder andere [A-Za-z0-9_] ("word character")
- \s staat voor onder andere [ \t\r\n\f\v] ("whitespace characters": space, tab en carriage-return, newline, form feed en vertical tab).

Ook "negative shorhand character classes" bestaan :

- \D staat voor [^\d] ("nondigit character")
- \W staat voor [^\w] ("nonword character")
- \S staat voor [^\s] ("nonwhitespace character")
Visual Basic 2012 Broncode - Codevoorbeeld 705
Imports System.Text.RegularExpressions
Class Example3
    Public Shared Sub Main()
        Dim input As String = "a_ .1"
        Example1.Print(input, "\w")  ' Any word character.
        Example1.Print(input, "\W")  ' Any nonword character.
        Example1.Print(input, "\s")  ' Any whitespace character.
        Example1.Print(input, "\S")  ' Any nonwhitespace character.
        Example1.Print(input, "\d")  ' Any digit (decimal digit).
        Example1.Print(input, "\D")  ' Any nondigit.
        '
        Console.ReadLine()
    End Sub
End Class
Console Application Output
"a_ .1" has match "a" at index 0 for pattern "\w"
"a_ .1" has match "_" at index 1 for pattern "\w"
"a_ .1" has match "1" at index 4 for pattern "\w"
"a_ .1" has match " " at index 2 for pattern "\W"
"a_ .1" has match "." at index 3 for pattern "\W"
"a_ .1" has match " " at index 2 for pattern "\s"
"a_ .1" has match "a" at index 0 for pattern "\S"
"a_ .1" has match "_" at index 1 for pattern "\S"
"a_ .1" has match "." at index 3 for pattern "\S"
"a_ .1" has match "1" at index 4 for pattern "\S"
"a_ .1" has match "1" at index 4 for pattern "\d"
"a_ .1" has match "a" at index 0 for pattern "\D"
"a_ .1" has match "_" at index 1 for pattern "\D"
"a_ .1" has match " " at index 2 for pattern "\D"
"a_ .1" has match "." at index 3 for pattern "\D"

28.4.4. Period Character Class

De period character class . staat voor alle karakters.
Visual Basic 2012 Broncode - Codevoorbeeld 706
Imports System.Text.RegularExpressions
Class Example4
    Public Shared Sub Main()
        Dim input As String = "ab.12*?"
        '
        Console.WriteLine("Input : ")
        Console.WriteLine(input)
        Console.WriteLine()
        '
        '
        Console.WriteLine("Matches for pattern ""."" : ")
        For Each m As Match In Regex.Matches(input, ".")
            Console.WriteLine("""" & m.Value & """ " & _
                              "at index " & m.Index.ToString())
        Next
        Console.WriteLine()
        '
        Console.WriteLine("Matches for pattern ""[.]"" : ")
        For Each m As Match In Regex.Matches(input, "[.]")
            Console.WriteLine("""" & m.Value & """ " & _
                              "at index " & m.Index.ToString())
        Next
        '
        Console.ReadLine()
    End Sub
End Class
Console Application Output
Input :
ab.12*?

Matches for pattern "." :
"a" at index 0
"b" at index 1
"." at index 2
"1" at index 3
"2" at index 4
"*" at index 5
"?" at index 6

Matches for pattern "[.]" :
"." at index 2
Logischerwijs verlies . zijn speciale betekenis tussen vierkante haakjes.

Verderop zullen we zien hoe . alle karakters onder de "singeline" optie resulteert.  Onder optie "multiline" zullen de newlines \s niet opgenomen worden in het resultaat.

Dit artikel is gepubliceerd op maandag 15 oktober 2012 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.