|
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") Example1.Print(input, "'") Example1.Print(input, "-") Example1.Print(input, ",") Example1.Print(input, "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 SubEnd ClassDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
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 KaraktersBepaalde 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, "\.") Example1.Print(input, "\$") Example1.Print(input, "\^") Example1.Print(input, "\{") Example1.Print(input, "\[") Example1.Print(input, "\(") Example1.Print(input, "\|") Example1.Print(input, "\)") Example1.Print(input, "\*") Example1.Print(input, "\+") Example1.Print(input, "\?") Example1.Print(input, "\\") Console.ReadLine() End SubEnd ClassDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
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.
|