Task 15. But wait! This chatbot it too simple you say? You notice that you must type the pattern to be matched EXACTLY or the chatbot will not respond appropriately? You changed your mind and you want a more advanced chatbot. OK. We will add the ability to use regular expressions in the pattern matching of your chatbot. Copy and paste the following code (taken from links referenced below and modified) and replace the DoesMatch function:
Public Function DoesMatch(Statement As String, RegExPattern As String) As Boolean
Dim re As RegExp
Dim matches As MatchCollection
Set re = New RegExp
re.IgnoreCase = True
re.Global = True
re.Pattern = RegExPattern
Set matches = re.Execute(Statement)
If matches.Count = 0 Then
DoesMatch = False
Exit Function
End If
DoesMatch = True
End Function
a. You will get an error: User Defined Type Not Allowed.
i. After stopping execution of your code and while still viewing the code for your form select the “Tools” option from the top menu bar. Select “References”. Scroll way down to the the references starting with “M” and check off the one called “Microsoft VBScript Regular Expressions 1.0”. Save. Close and reopen your form.
b. See link here: http://stackoverflow.com/questions/5539141/microsoft-office-access-like-vs-regex
c. For syntax info see here: http://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx
d. For a simple reference see here: http://www.alertsite.com/help/RegexMatching.html
e. Google regular expressions for more info than you ever wanted on regular expressions.
f. Try entering “.*\bcat\b.*” as a pattern and “You used the word cat in a sentence.” as the response.
g. Try entering “.*\cat.*” as a pattern to match anything starting with “cat” so this would match “cat” and “catherine”.
h. Try entering “.*\cat\b.*\bdog\b.*” as a pattern to match any sentence with the word “cat” in it followed by the word “dog”.
Now the sky’s the limit!