-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlesson9.html
120 lines (118 loc) · 5.43 KB
/
lesson9.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
<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 9: Matching specific characters</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>9. Matching specific characters</h2>
<p>
Sometimes, it's not enough to state that any word character may be present;
you want to specify which characters are allowed.
We can use the square brackets <span class="regex">[]</span> for this.
For example, the regex <span class="regex">c[au]t</span> will match
"cat" and "cut" ('a "c" followed by either an "a" or a "u", followed by a "t"').
</p>
<p>
Sometimes, you also want to express the opposite: any character except X, Y or Z.
To do this, you add the caret character "^" after the opening square bracket.
For example, the regex <span class="regex">la[^pgd]</span>
will match "lab" and "lax" but not "lap", "lag" and "lad".
</p>
<p>
Note that inside the square brackets all special characters except "\" and "]"
lose their special powers: <span class="regex">[.,]</span> will match only a
dot and a comma, not "any character except a new line". If you want to include
a literal backslash or a literal closing square bracket among the options,
you have to escape those:
<span class="regex">[[\]\\]</span> will match either an opening bracket, a closing bracket or a backslash.
</p>
</div>
<div id="exercise" type="full_match"> <!-- possible types: full_match, partial_match -->
<div id="exercise_title">
Exercise 8: match all statements about people born between 1820 and 1828 (including 1820 and 1828):
</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="born in 1820">born in 1820</td>
<td class="correctOrNot"></td>
</tr>
<tr>
<td class="instruction">Don't match this string</td>
<td class="sample" match="died in 1825">died in 1825</td>
<td class="correctOrNot"></td>
</tr>
<tr>
<td class="instruction">Match this string entirely</td>
<td class="sample" match="born in 1821">born in 1821</td>
<td class="correctOrNot"></td>
</tr>
<tr>
<td class="instruction">Match this string entirely</td>
<td class="sample" match="Born in 1828">Born in 1828</td>
<td class="correctOrNot"></td>
</tr>
<tr>
<td class="instruction">Don't match this string</td>
<td class="sample" match="died in 1828">died in 1828</td>
<td class="correctOrNot"></td>
</tr>
<tr>
<td class="instruction">Don't match this string</td>
<td class="sample" match="born in 1829">born in 1829</td>
<td class="correctOrNot"></td>
</tr>
<tr>
<td class="instruction">Don't match this string</td>
<td class="sample" match="born in 1814">born in 1814</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="[Bb]orn in 182[012345678]"></input>
<input id="next_button" type="button" onclick="location.href='lesson10.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;">
First of all, write down which characters are common to each of the strings
we need to match.<br/>
Then use the square brackets to list all the possibilities
for the characters that differ<br/>
(capital or lower case b; the numbers from 0 to 8).
</div>
<div id="solution" style="display:none;">
<span class="regex">[Bb]orn in 182[012345678]</span><br/>
'Either an upper-case or a lower-case letter b, <br/>
followed by the string "orn in", followed by a space, <br/>
followed by the numbers 1, 8 and 2,<br/>
followed by either a 0,1,2,3,4,5,6,7, or 8' <br/>
</div>
</div>
<input id="back_button" type="button" onclick="location.href='lesson8.html';" value="Back"/>
<script type="text/javascript" src="js/script.js"></script>
</body>
</html>