Untitled Snippet

Send Snippet To: Save this snippet to Code Collector Pro -- view all surfrgeek's snippets
language: Other
license: Other

Code for Snippet:

                
Option Explicit
 
'#1
'MATCHING ONLY Examples using the TEST method
'********************************************
 
Public Sub Matching_1()
    'declare a RegExp type
    Dim Reg As RegExp
 
    'declare other variables
    Dim str As String
    Dim valid As Boolean
 
    'create a new RegExp object
    Set Reg = New RegExp
 
    'set string to be tested
    'str = "John"
    str = InputBox("Enter string in the format Xxxxx (e.g., John)", "Enter String")
    'set the desired pattern in the RegExp object
    Reg.Pattern = "^[A-Z][a-z]+$"
 
    'conduct testing
    valid = Reg.Test(str)   'Use Test method of the object
 
    MsgBox valid
 
End Sub
 
'#2
'MATCHING ONLY Examples using the TEST method
'********************************************
 
Public Sub Matching_2()
    'declare a RegExp type
    Dim Reg As RegExp
 
    'declare other variables
    Dim str As String
    Dim valid As Boolean
 
    'create a new RegExp object
    Set Reg = New RegExp
 
    'set string to be tested
    'str = "John123"
    str = InputBox("Enter string in the format Xxxxnnn (e.g. John123)", "Enter String")
    'set the desired pattern in the RegExp object
    Reg.Pattern = "^[A-Z][a-z]+\d\d\d$"
 
    'conduct testing
    valid = Reg.Test(str)   'Use Test method of the object
 
    MsgBox valid
 
End Sub
 
 
'#3
'MATCHING ONLY Examples using the TEST method
'********************************************
 
Public Sub Matching_3()
    'declare a RegExp type
    Dim Reg As RegExp
 
    'declare other variables
    Dim str As String
    Dim valid As Boolean
 
    'create a new RegExp object
    Set Reg = New RegExp
 
    'set string to be tested
    'str = "123 rose street north west"
    str = InputBox("Enter address", "Enter Address")
    'set the desired pattern in the RegExp object
    Reg.Pattern = "^[0-9]+ ([A-Za-z]+ [A-Za-z]+)+$"
    'Reg.Pattern = "^[0-9]+ ([A-Za-z]+ [A-Za-z]+)+ \d{5,5}$"
 
    'conduct testing
    valid = Reg.Test(str)   'Use Test method of the object
 
    MsgBox valid
 
End Sub
 
'#4
'MATCHING ONLY Examples using the TEST method
'********************************************
 
Public Sub Matching_4()
    'declare a RegExp type
    Dim Reg As RegExp
 
    'declare other variables
    Dim str As String
    Dim valid As Boolean
 
    'create a new RegExp object
    Set Reg = New RegExp
 
    'set string to be tested
    'str = "chicago a a a"
    str = InputBox("Enter a string", "Enter String")
    'set the desired pattern in the RegExp object
    Reg.Pattern = "^[A-Za-z]+( [A-Za-z]+)*$"
 
    'conduct testing
    valid = Reg.Test(str)   'Use Test method of the object
 
    MsgBox valid
 
End Sub
 
'#5
'MATCHING ONLY Examples using the TEST method
'********************************************
Public Sub Matching_5()
    'declare a RegExp type
    Dim Reg As RegExp
 
    'declare other variables
    Dim str As String
    Dim valid As Boolean
 
    'create a new RegExp object
    Set Reg = New RegExp
 
    'set string to be tested
    str = InputBox("Enter a valid Region Number (e.g., Region 13).", "Enter Region Number")
    'set the desired pattern in the RegExp object
    Reg.Pattern = "^Region \d{1,3}$"
 
    'conduct testing
    valid = Reg.Test(str)   'Use Test method of the object
 
    MsgBox valid
 
End Sub
 
'#6
'MATCHING ONLY Examples using the TEST method
'********************************************
'Password is 8 characters long
'Must start with an uppercase letter
'Followed by only uppercase letters and digits
 
Public Sub Matching_6()
    'declare a RegExp type
    Dim Reg As RegExp
 
    'declare other variables
    Dim str As String
    Dim valid As Boolean
 
    'create a new RegExp object
    Set Reg = New RegExp
 
    'set string to be tested
    str = InputBox("Enter a valid Password.", "Enter Password")
    'set the desired pattern in the RegExp object
    Reg.Pattern = "^[A-Z][A-Z0-9]{7,7}$"
 
    'conduct testing
    valid = Reg.Test(str)   'Use Test method of the object
 
    MsgBox valid
 
End Sub
 
'#7
'MATCHING ONLY Examples using the TEST method
'********************************************
'Test 10-digit or 13-digit ISBN numbers
 
Public Sub Matching_7()
    'declare a RegExp type
    Dim Reg As RegExp
 
    'declare other variables
    Dim str As String
    Dim valid As Boolean
 
    'create a new RegExp object
    Set Reg = New RegExp
 
    'set string to be tested
    str = InputBox("Enter a 10-digit or 13-digit ISBN.", "ISBN")
    'set the desired pattern in the RegExp object
    Reg.Pattern = "^\d{10,10}|\d{13,13}$"
 
    'conduct testing
    valid = Reg.Test(str)   'Use Test method of the object
 
    MsgBox valid
 
End Sub
 
'#8
'MATCHING ONLY Examples using the TEST method
'********************************************
'Enter phone number in the form (xxx)xxx-xxxx
 
Public Sub Matching_8()
    'declare a RegExp type
    Dim Reg As RegExp
 
    'declare other variables
    Dim str As String
    Dim valid As Boolean
 
    'create a new RegExp object
    Set Reg = New RegExp
 
    'set string to be tested
    str = InputBox("Enter Phone number in the format (xxx)xxx-xxxx.", "Phone")
    'set the desired pattern in the RegExp object
    Reg.Pattern = "^\(\d{3,3}\)\d{3,3}\-\d{4,4}$"
 
    'conduct testing
    valid = Reg.Test(str)   'Use Test method of the object
 
    MsgBox valid
 
End Sub
 
'#9
'MATCHING ONLY Examples using the TEST method
'********************************************
'Does the string have a vowel?
 
Public Sub Matching_9()
    'declare a RegExp type
    Dim Reg As RegExp
 
    'declare other variables
    Dim str As String
    Dim valid As Boolean
 
    'create a new RegExp object
    Set Reg = New RegExp
 
    'set string to be tested
    str = InputBox("Enter a string.", "Vowel")
    'set the desired pattern in the RegExp object
    Reg.Pattern = "[AEIOUaeiou]"
 
    'conduct testing
    valid = Reg.Test(str)   'Use Test method of the object
 
    MsgBox valid
 
End Sub
 
'#10
'MATCHING ONLY Examples using the TEST method
'********************************************
'valid MSU email in the OLD System; for example: <[email protected]>?
 
Public Sub Matching_10()
    'declare a RegExp type
    Dim Reg As RegExp
 
    'declare other variables
    Dim str As String
    Dim valid As Boolean
 
    'create a new RegExp object
    Set Reg = New RegExp
 
    'set string to be tested
    str = InputBox("Enter an e-mail.", "Email")
    'set the desired pattern in the RegExp object
    Reg.Pattern = "[A-za-z]+(\.)?[A-za-z]+\d{0,2}@murraystate.edu"
 
    'conduct testing
    valid = Reg.Test(str)   'Use Test method of the object
 
    MsgBox valid
 
End Sub
 
'#11
'MATCHING ONLY Examples using the TEST method
'********************************************
'CASE: Test Password Policy:
    'Must have exactly 8 characters (alphabets and numeric digits only, no spaces and special characters allowed)
    'Must begin with a capital letter
    'Remainder 7 can be capital letters or digits (but not more than 2 digits)
'
Public Sub Matching_11()
    'declare a RegExp type
    Dim Reg As RegExp
 
    'declare other variables
    Dim str As String
    Dim valid As Boolean
 
 
    'create a new RegExp object
    Set Reg = New RegExp
 
    'set string to be tested
    str = InputBox("Enter Password following the given policy", "Enter Password")
 
    'set the desired pattern in the RegExp object
    Reg.Global = True
 
    'Write the first pattern to look ahead and do preliminary checking first
    'Look ahead and test if "total of 8 charactes" and "includes only capital letters and idgits".
    'The look ahead symbols look like "(?=    )"
 
    Reg.Pattern = "(?=^[A-Z0-9]{8}$)"
 
    'Write the second pattern to check the sequence within the 8-character limit
    'Combine the two patterns with AND "&"
    Reg.Pattern = Reg.Pattern & "^[A-Z]+\d{0,1}[A-Z]*\d{0,1}[A-Z]*$"
 
    'conduct testing
    valid = Reg.Test(str)   'Use Test method of the object
 
    MsgBox valid
 
 
 
End Sub
 
comments powered by Disqus

Info

Link to this snippet:


Download to Code Collector

To use the direct link to your snippet on CodeCollector.net either copy the html from the above section or drag the Download to Code Collector to where you would like to use it.

More Info:

Times Viewed: 42
Date Added: 2017-09-09 19:09:54
Last Modified: 2017-09-09 14:10:09

Web Analytics