Powered by: Dharamveer Saxena

Tuesday, June 29, 2010

How to use Regular Expression to test Email ID and other strings


A regular expression can be used as follow:

'Here x is the string that needed to be tested and y is the pattern against which x will be tested


Function RegExpression(x,y)
Dim reg
Set reg = New RegExp
reg.Pattern = y ' Regular Expression filter
reg.IgnoreCase = True 'ignoring the case sensitive
RegExpression = reg.Test(x) ' Test the captured value against the filter
End Function


The above function can be used in the main procedure as:

Sub RegularExpression
x = "0.10" ' Captured value
y = "0[.]10" ' Regular expression filter and expected value
z = RegExpression(x,y) ' Call the function
if z = -1 then
log.message "The value is valid"
else
log.error "The value is not valid"
end if
End Sub


'An email ID can be tested with Regular expression using the above function by passing the following patern:

'here y can change as per the characters allowed in the email ID

Sub RegularExpression
x = "abc27@yahoo.com" ' Captured value
y = "^[A-Z0-9]+@[A-Z0-9]+.[A-Z]{3}" ' Regular expression filter and expected value
z = RegExpression(x,y) ' Call the function
if z = -1 then
log.message "The value is valid"
else
log.error "The value is not valid"
end if
End Sub

No comments:

Post a Comment