Beruflich Dokumente
Kultur Dokumente
Validation
Validation refers to ensuring entered data is well, valid.
Validation helps avoid bugs by filtering out invalid data to
get in to the program flow.
Type test - This test checks if the input is the correct type. For
example: Integers of dates that do not need conversion afterwards.
Range test - This tests checks to see if entered values are within a
specified range. For example : If you are only supposed to type in
50 characters, you must not be able to enter more than fifty.
Regular Expressions
Regular Expressions
RegEx - Identifying
Duplicate Words
Imports
System.Text.RegularExpressions
Module
modMain
Public
Sub
Main()
Dim
pattern
As
String
=
"\b(\w+?)\s\1\b"
Dim
input
As
String
=
"This
this
is
a
nice
day.
What
about
this?
This
tastes
good.
I
saw
a
a
dog."
For
Each
match
As
Match
In
Regex.Matches(input,
pattern,
RegexOptions.IgnoreCase)
Console.WriteLine("{0}
(duplicates
'{1}')
at
position
{2}",
_
match.Value,
match.Groups(1).Value,
match.Index)
Next
End
Sub
End
Module
'
The
example
displays
the
following
output:
'
This
this
(duplicates
'This)'
at
position
0
'
a
a
(duplicates
'a)'
at
position
66
RegEx
The regular expression pattern \b(\w+?)\s\1\b can be interpreted as
follows:
\b
Start at a word boundary.
(\w+?)
Match one or more word characters, but as few characters as possible.
Together, they form a group that can be referred to as \1.
\s
Match a white-space character.
\1
Match the substring that is equal to the group named \1.
\b
Match a word boundary.
RegEx Methods
Guided Example
Example
Imports System.Text.RegularExpressions ' Regular Expressions Namespace
Private
Private
Private
Private
End If
End Sub
'Not A Match
If Not reSurname.IsMatch(txtSurname.Text) Then
txtName.Focus()
txtName.Clear()
SurnameValid = False
Else
SurnameValid = True
End If
End Sub
'Something Typed In
If Not String.IsNullOrEmpty(strPhoneNum) Then
Else
End If
End Function
Private Sub txtTel_LostFocus(sender As Object, e As System.EventArgs) Handles txtTel.LostFocus
End If
End Sub
'Set Up Reg Exp Pattern To Allow Most Characters, And No Special Characters
Dim reEmail As Regex = New Regex("([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\." + _
")|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})", _
RegexOptions.IgnoreCase _
Or RegexOptions.CultureInvariant _
Or RegexOptions.IgnorePatternWhitespace _
Or RegexOptions.Compiled _
)
If blnPossibleMatch Then
Else
End If
End If
End Sub
Private Sub txtEmail_LostFocus(sender As Object, e As System.EventArgs) Handles txtEmail.LostFocus
End Sub
References:
MSDN
Code guru