Skip to content

Commit 933b03a

Browse files
Add tests to labs regex0 and regex1
Signed-off-by: David A. Wheeler <[email protected]>
1 parent 4614921 commit 933b03a

File tree

3 files changed

+69
-38
lines changed

3 files changed

+69
-38
lines changed

docs/labs/regex0.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ <h2>Background</h2>
4343
<li>Another rule is that square brackets surround a rule that specifies any of a number of characters. If the square brackets surround just alphanumerics, then the pattern matches any of them. So <tt>[brt]</tt> matches a single “<tt>b</tt>”, “<tt>r</tt>”, or “<tt>t</tt>”.
4444
Inside the brackets you can include
4545
ranges of symbols separated by dash ("-"), so
46-
<tt>[A-D]</tt> will match one character, which can be one A, one B, one C,
47-
or one D.
48-
You can do this more than once.
46+
<tt>[A-D]</tt> will match one character in that range,
47+
which can be one A, one B, one C, or one D.
48+
You can provide more than one range inside brackets.
4949
For example,
5050
the term <tt>[A-Za-z]</tt> will match one character, which can be
5151
an uppercase Latin letter or a lowercase Latin letter.
52-
(This text assumes you're not using a long-obsolete character system
53-
like EBCDIC.)
52+
(For purposes of our labs we're
53+
assuming you're not using a long-obsolete character system like EBCDIC.)
5454
<li>If you follow a pattern with “<tt>&#42;</tt>”, that means
5555
<i>0 or more times</i>”.
5656
In almost all regex implementations (except POSIX BRE),

docs/labs/regex0.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ info =
3131
[ "x" ],
3232
],
3333
},
34+
{
35+
present: String.raw`\[(cat|act)\]`,
36+
text: "The pattern '[cat]' or '[act]' matches one latter, a 'c', an 'a', or 't'. That is not what you want.",
37+
examples: [
38+
[ "[cat]" ],
39+
[ "[act]" ],
40+
],
41+
},
3442
{
3543
absent: "cat",
3644
text: "The pattern \"cat\" is needed to search for \"cat\".",
@@ -43,24 +51,24 @@ info =
4351
index: 1,
4452
text: "You need to mention A.",
4553
examples: [
46-
[ "cat", "B" ],
54+
[ null, "B" ],
4755
],
4856
},
4957
{
5058
absent: String.raw`A(\+|A\*)`,
5159
index: 1,
5260
text: "Use \"A+\" to indicate \"one or more A\". You could also write \"AA*\".",
5361
examples: [
54-
[ "cat", "A" ],
55-
[ "cat", "AA" ],
62+
[ null, "A" ],
63+
[ null, "AA" ],
5664
],
5765
},
5866
{
5967
absent: "B",
6068
index: 1,
6169
text: "You need to mention B.",
6270
examples: [
63-
[ "cat", "A+" ],
71+
[ null, "A+" ],
6472
],
6573
},
6674
{
@@ -69,7 +77,7 @@ info =
6977
index: 1,
7078
text: "Use \"B+\" to indicate \"one or more B\". You could also write \"BB*\".",
7179
examples: [
72-
[ "cat", "A+B" ],
80+
[ null, "A+B" ],
7381
],
7482
},
7583
],

docs/labs/regex1.js

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,161 +5,184 @@ info =
55
present: "/",
66
text: "In JavaScript a constant regular expression is surrounded by forward slashes like /PATTERN/. However, for this exercise we only want the text inside the slashes (the pattern itself).",
77
examples: [
8-
[ "/" ]
8+
[ "/" ],
99
],
1010
},
1111
{
1212
present: "[\"'`]",
1313
text: "In this exercise we only want the regular expression pattern itself. There is no need to use any kind of quote mark.",
1414
examples: [
15-
[ "'" ]
15+
[ "'" ],
1616
],
1717
},
1818
{
1919
absent: String.raw`^\^`,
2020
text: "For input validation, start with '^' to indicate a full match.",
2121
examples: [
22-
[ "(Y|N)" ]
22+
[ "(Y|N)" ],
2323
],
2424
},
2525
{
2626
present: String.raw`\\[Zz]`,
2727
text: "The ECMAScript (JavaScript) specification does not support \\Z or \\z.",
2828
examples: [
29-
[ "^Y|N\\z" ]
29+
[ "^Y|N\\z" ],
3030
],
3131
},
3232
{
3333
absent: String.raw`\$$`,
3434
text: "For input validation, end with '$' to indicate a full match.",
3535
examples: [
36-
[ "^(Y|N)" ]
36+
[ "^(Y|N)" ],
3737
],
3838
},
3939
{
4040
absent: String.raw`[\|\[]`,
4141
text: "Consider using [YN], to match either a Y or an N.",
4242
examples: [
43-
[ "^$" ]
43+
[ "^$" ],
4444
],
4545
},
4646
{
4747
present: String.raw`\|`,
4848
absent: String.raw`\(`,
4949
text: "If you use \"|\" you must parentheses or the precedence will be wrong. For example, \"^A|B$\" accepts anything beginning with A, and it also accepts anything ending with B. That is not what you want.",
5050
examples: [
51-
[ "^Y|N$" ]
51+
[ "^Y|N$" ],
5252
],
5353
},
5454
{
5555
present: " ",
5656
text: "Spaces normally match spaces in a regex. Do not use them in this case, because a space is not one of the allowed characters.",
5757
examples: [
58-
[ "^[YN] $" ]
58+
[ "^[YN] $" ],
5959
],
6060
},
6161
{
6262
absent: String.raw`^\^`,
6363
index: 1,
6464
text: "For input validation, start with '^' to indicate a full match.",
6565
examples: [
66-
[ "^[YN]$", "" ]
66+
[ null, "" ],
6767
],
6868
},
6969
{
7070
absent: String.raw`\$$`,
7171
index: 1,
7272
text: "For input validation, end with '$' to indicate a full match.",
7373
examples: [
74-
[ "^[YN]$", "^" ]
74+
[ null, "^" ],
7575
],
7676
},
7777
{
7878
absent: String.raw`\[A-Z\]`,
7979
index: 1,
8080
text: "You can use [A-Z] to match one uppercase Latin letter (A through Z).",
8181
examples: [
82-
[ "^[YN]$", "^$" ]
82+
[ null, "^$" ],
8383
],
8484
},
8585
{
8686
present: String.raw`\^\[A-Z\]\*`,
8787
index: 1,
88-
text: "A \"*\" matches one or more, not one or more."
88+
text: "A \"*\" matches one or more, not one or more.",
89+
examples: [
90+
[ null, "^[A-Z]*$" ],
91+
],
8992
},
9093
{
9194
present: String.raw`\(`,
9295
index: 1,
93-
text: "You do not need parentheses to solve this problem."
96+
text: "You do not need parentheses to solve this problem.",
97+
examples: [
98+
[ null, "^([A-Z])+$" ],
99+
],
94100
},
95101
{
96-
absent: String.raw`(\[A-Z\]\+|
97-
\[A-Z\]\[A-Z\]\*)`,
102+
absent: String.raw`\[A-Z\](\+|\[A-Z\]\*)`,
98103
index: 1,
99104
text: "You can use [A-Z]+ to match one or more uppercase Latin letters.",
100105
examples: [
101-
[ "^[YN]$", "^[A-Z]$" ]
106+
[ null, "^[A-Z]$" ],
102107
],
103108
},
104109
{
105110
present: "True",
106111
index: 2,
107-
text: "Regular expressions are case-sensitive by default; use \"true\"."
112+
text: "Regular expressions are case-sensitive by default; use \"true\"",
113+
examples: [
114+
[ null, null, "True" ],
115+
],
108116
},
109117
{
110118
present: "False",
111119
index: 2,
112-
text: "Regular expressions are case-sensitive by default; use \"false\"."
120+
text: "Regular expressions are case-sensitive by default; use \"false\".",
121+
examples: [
122+
[ null, null, "False" ],
123+
],
113124
},
114125
{
115126
absent: String.raw`\|`,
116127
index: 2,
117-
text: "Use \"|\" to express alternatives."
128+
text: "Use \"|\" to express alternatives.",
118129
},
119130
{
120131
present: String.raw`^\^true\|false\$$`,
121132
index: 2,
122-
text: "No. This would match anything beginning with the term true, or anything ending with the term false. Use parenthesis."
133+
text: "No. This would match anything beginning with the term true, or anything ending with the term false. Use parenthesis.",
123134
},
124135
{
125136
present: String.raw`^\^false\|true\$$`,
126137
index: 2,
127-
text: "No. This would match anything beginning with the term false, or anything ending with the term true. Use parenthesis."
138+
text: "No. This would match anything beginning with the term false, or anything ending with the term true. Use parenthesis.",
128139
},
129140
{
130141
absent: String.raw`\(`,
131142
index: 2,
132-
text: "Use parentheses."
143+
text: "Use parentheses.",
133144
},
134145
{
135146
present: String.raw`\$`,
136147
index: 3,
137-
text: "This is Python, not ECMAScript (JavaScript). Use \\Z at the end, not $."
148+
text: "This is Python, not ECMAScript (JavaScript). Use \\Z at the end, not $.",
149+
examples: [
150+
[ null, null, null, "^[A-Z]+$"],
151+
],
138152
},
139153
{
140154
present: String.raw`\\z`,
141155
index: 3,
142-
text: "This is Python. Use \\Z at the end, not \\z."
156+
text: "This is Python. Use \\Z at the end, not \\z.",
157+
examples: [
158+
[ null, null, null, "^[A-Z]+\\z"],
159+
],
143160
},
144161
{
145162
absent: String.raw`^\\A`,
146163
index: 4,
147-
text: "This is Ruby. Use \\A at the beginning."
164+
text: "This is Ruby. Use \\A at the beginning.",
165+
examples: [
166+
[ null, null, null, null, "^[A-Z]+$"],
167+
],
148168
},
149169
{
150170
absent: String.raw`\\z$`,
151171
index: 4,
152-
text: "This is Ruby. Use \\z at the end."
172+
text: "This is Ruby. Use \\z at the end.",
173+
examples: [
174+
[ null, null, null, null, "\\A[A-Z]+$"],
175+
],
153176
},
154177
{
155178
absent: String.raw`\[A-Z\]`,
156179
index: 4,
157-
text: "Use [A-Z] to match one uppercase Latin letter."
180+
text: "Use [A-Z] to match one uppercase Latin letter.",
158181
},
159182
{
160183
present: String.raw`\[A-Z\](\*|\+)`,
161184
index: 4,
162-
text: "In this case we are only matching one letter, not many of them. Do not use \"*\" or \"+\" after [A-Z]."
185+
text: "In this case we are only matching one letter, not many of them. Do not use \"*\" or \"+\" after [A-Z].",
163186
},
164187
],
165188
expected: [

0 commit comments

Comments
 (0)