-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlesson7.html
125 lines (123 loc) · 5.87 KB
/
lesson7.html
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<html>
<head>
<meta charset="utf-8">
<meta name="application-name" content="KITAB regex tutorial">
<meta name="created" content="2021-05-05">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>KITAB regex tutorial - lesson 7: Quantification</title>
<link rel="stylesheet" type="text/css" href="css/styles.css" />
<!--<script src="https://unpkg.com/xregexp/xregexp-all.js"></script>--> <!--regular expressions library with support for unicode code blocks-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/xregexp/3.2.0/xregexp-all.min.js"></script>
</head>
<body>
<h1>Regex tutorial for humanists</h1>
<div id="tutorial">
<h2>7. Quantification: how often can a character be present?</h2>
<p>
Very often, we want to make a character optional in our patterns.
For example, in English, you might
be searching for where a text speaks about cats, in singular or plural.
This can be expressed using the special character <span class="regex">?</span>,
which means that a character could be present zero or one time: <span class="regex">cats?</span> will match both "cat" and "cats".
</p>
<p>
Sometimes, however, you might want to express that a character should
be present at least once, but perhaps more times.
This can be done using the special character <span class="regex">+</span>.
For example, if you want to match two alternative spellings of the name
Mathew (Mathew and Matthew), you could use the regex
<span class="regex">Mat+hew</span>. You could also match all dates
in a document using the regex <span class="regex">\d+/\d+/\d+</span>:
this would match "12/10/2022", "2/2/23", "2019/12/31", etc.
</p>
<p>
In other cases, you might want to indicate that a character could be present
zero or more times.
This is expressed using the character <span class="regex">*</span>.
You might, for example, be interested in all mentions of a word in Arabic,
with all possible prefixes that are possible.
The regex <span class="regex">\w*كتاب</span> will match
"كتاب",
"الكتاب",
"والكتاب",
"فبالكتاب", ...
</p>
<p>
Combine the use of the three quantifiers <span class="regex">?</span>,
<span class="regex">+</span> and <span class="regex">*</span> to
write a pattern that matches the examples of email addresses below.
</p>
</div>
<div id="exercise" type="full_match"> <!-- possible types: full_match, partial_match -->
<div id="exercise_title">
Exercise 7: match any string below that consists of a valid email address.
</div>
<div id="samples">
<table>
<thead>
<tr>
<th>Task</th>
<th>Text</th>
<th> </th>
</tr>
<!-- line below the table heading: -->
<tr><td style="border-top:1px solid black;" colspan="3"></td></tr>
</thead>
<tbody>
<tr>
<td class="instruction">Match this string entirely</td>
<td class="sample" match="[email protected]">[email protected]</td>
<td class="correctOrNot"></td>
</tr>
<tr>
<td class="instruction">Don't match this string</td>
<td class="sample" match="[email protected]">[email protected]</td>
<td class="correctOrNot"></td>
</tr>
<tr>
<td class="instruction">Match this string entirely</td>
<td class="sample" match="[email protected]">[email protected]</td>
<td class="correctOrNot"></td>
</tr>
<tr>
<td class="instruction">Match this string entirely</td>
<td class="sample" match="[email protected]">[email protected]</td>
<td class="correctOrNot"></td>
</tr>
<tr>
<td class="instruction">Don't match this string</td>
<td class="sample" match="">www.gmail.com</td>
<td class="correctOrNot"></td>
</tr>
</tbody>
</table>
</div>
<div id="input">
<input id="solution_input" autocomplete="off" autocapitalize="off" autocorrect="off" spellcheck="false" placeholder="Type your regex pattern" solution="\w+\.?\w*@\w+\.\w+"></input>
<input id="next_button" type="button" onclick="location.href='lesson8.html';" value="Next lesson" disabled title="Get the solution first!"/>
</div>
<div id="hintOrSolution">
<input id="hint_button" type="button" onclick="showHint()" value="Hint" title="Get a hint"/>
<input id="solution_button" type="button" onclick="showSolution()" value="Solution" title="Take the hint before you get the solution!" disabled/>
</div>
<div id="hint" style="display:none;">
All relevant solutions start with <b>more than one</b> alphanumeric character
(letters and numbers), <b>sometimes</b> separated by a single full stop,
followed by an at sign, and then <b>more than one</b> letter,
a single full stop, and then again <b>more than one</b> letter.
Use the relevant quantifier for each of the statements in bold!
</div>
<div id="solution" style="display:none;">
The name part of the email address can be conceived as a sequence of one or more alphanumeric characters
<span class="regex">\w+</span>, followed by zero or one literal dot <span class="regex">\.?</span>,
followed by zero or more alphanumeric characters <span class="regex">\w*</span>
<br/>
Remember that <span class="regex">+</span> stands for "one or more",
<span class="regex">*</span> for "zero or more",
and <span class="regex">?</span> for "zero or one" repetition.
</div>
</div>
<input id="back_button" type="button" onclick="location.href='lesson6.html';" value="Back"/>
<script type="text/javascript" src="js/script.js"></script>
</body>
</html>