forked from UUDeCART/decart_rule_based_nlp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregular_expression_quiz.py
36 lines (26 loc) · 1.47 KB
/
regular_expression_quiz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import re
def which_a_string_not_matched(s):
if not isinstance(s, str):
return 'Please pass a string to this function.'
if s == 'a':
return 'The string "a" is the correct answer. Since the function uses the + quantifier that means there must be at least one "h" character after an "a"'
if 'a' not in s:
return 'There should be at least one character "a" in your answer since the regular expression expects one "a"'
else:
return 'The + quantifier means that there will be one or more the character that it modifies to its left. Please try again.'
def test_infiltrates_expression(s):
if not isinstance(s, str):
return 'Please pass a string for a regular expression to this function.'
expected_matches = ['infiltrate', 'infiltrates']
unexpected_matches = ['infiltrat', 'infiltratess']
for expected in expected_matches:
matches = re.findall(s, expected)
if len(matches) == 0:
return 'INCORRECT. Your expression failed to match the string [{0}]. Please try again.'.format(expected)
for unexpected in unexpected_matches:
matches = re.findall(s, unexpected)
if len(matches) > 0:
return 'INCORRECT. Your expression matched an unexpected string : [{0}]. Please try again.'.format(unexpected)
if '?' not in s:
return 'Hmmm... While not required, your expression did not use a "?". Please try using a regular expression which the special character "?" which will help in this task'
return 'CORRECT. Your expression seemed to match expected results.'