Skip to content

Commit f66b60f

Browse files
regex1: Add part 5
Signed-off-by: David A. Wheeler <[email protected]>
1 parent 002abee commit f66b60f

File tree

1 file changed

+73
-4
lines changed

1 file changed

+73
-4
lines changed

docs/labs/regex1.html

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
\^(
2222
\[YN\]|
2323
\[NY\]|
24-
\(Y\|N\)|
25-
\(N\|Y\)
24+
\( (\?\:)? Y\|N \)|
25+
\( (\?\:)? N\|Y \)
2626
)\$
2727
</script>
2828

@@ -46,7 +46,8 @@
4646

4747
<!-- Full pattern of correct answer -->
4848
<script id="correct2" type="plain/text">
49-
\^\((
49+
\^\( (\?\:)?
50+
(
5051
true\|false|
5152
false\|true
5253
)\)\$
@@ -65,6 +66,25 @@
6566
\\Z
6667
</script>
6768

69+
<!-- Sample expected answer.
70+
Only matches one Latin letters (A through Z), followed by a
71+
dash ("-"), followed by one or more digits.
72+
This time, do it for Ruby (not JavaScript or Python).
73+
-->
74+
<script id="expected4" type="plain/text">
75+
\A[A-Z]-[0-9]+\z
76+
</script>
77+
78+
<!-- Full pattern of correct answer -->
79+
<script id="correct4" type="plain/text">
80+
\\A
81+
\[A-Z\]
82+
-
83+
(\[0-9\]|\\d)
84+
(\+|(\[0-9\]|\\d)\*)
85+
\\z
86+
</script>
87+
6888
<script id="info" type="application/yaml">
6989
---
7090
hints:
@@ -93,10 +113,13 @@
93113
absent: |-
94114
\(
95115
text: If you use "|" you must parentheses or the precedence will be wrong.
116+
For example, "^A|B$" accepts anything beginning with A, and it also
117+
accepts anything ending with B. That is not what you want.
96118
examples:
97119
- - "^Y|N$"
98120
- present: " "
99-
text: Spaces normally match spaces in a regex; do not use them in this case.
121+
text: Spaces normally match spaces in a regex. Do not use them in this case,
122+
because a space is not one of the allowed characters.
100123
examples:
101124
- - "^[YN] $"
102125
- absent: |-
@@ -150,6 +173,16 @@
150173
\|
151174
index: 2
152175
text: Use "|" to express alternatives.
176+
- present: |-
177+
^\^true\|false\$$
178+
index: 2
179+
text: No. This would match anything beginning with the term true,
180+
or anything ending with the term false. Use parenthesis.
181+
- present: |-
182+
^\^false\|true\$$
183+
index: 2
184+
text: No. This would match anything beginning with the term false,
185+
or anything ending with the term true. Use parenthesis.
153186
- absent: |-
154187
\(
155188
index: 2
@@ -162,6 +195,23 @@
162195
\\z
163196
index: 3
164197
text: This is Python. Use \Z at the end, not \z.
198+
- absent: |-
199+
^\\A
200+
index: 4
201+
text: This is Ruby. Use \A at the beginning.
202+
- absent: |-
203+
\\z$
204+
index: 4
205+
text: This is Ruby. Use \z at the beginning.
206+
- absent: |-
207+
\[A-Z\]
208+
index: 4
209+
text: Use [A-Z] to match one uppercase Latin letter.
210+
- present: |-
211+
\[A-Z\](\*|\+)
212+
index: 4
213+
text: In this case we are only matching one letter, not many of them.
214+
Do not use "*" or "+" after [A-Z].
165215
# successes:
166216
# - - " query ( 'id' ) . isInt ( {min: 1 , max: 9999 } ) ,"
167217
# - - " query ( `id` ) . isInt ( {min: 1 , max: 9_999 } ) , "
@@ -292,6 +342,25 @@ <h3>Part 4</h2>
292342
<button type="button" class="giveUpButton">Give up</button>
293343
</form>
294344

345+
<h3>Part 5</h2>
346+
<p>
347+
Create a regular expression
348+
that only matches one Latin letter (A through Z), followed by a
349+
dash ("-"), followed by one or more digits.
350+
This time, do it for Ruby (not JavaScript or Python).
351+
<!--
352+
You can use this an example for new labs.
353+
For multi-line inputs, instead of <input id="attempt0" type="text" ...>, use
354+
<textarea id="attempt" rows="2" cols="70">...</textarea>
355+
-->
356+
<form id="lab5">
357+
<pre></code>
358+
<input id="attempt4" type="text" size="70" spellcheck="false" value="">
359+
</code></pre>
360+
<button type="button" class="hintButton">Hint</button>
361+
<button type="button" class="resetButton">Reset</button>
362+
<button type="button" class="giveUpButton">Give up</button>
363+
</form>
295364

296365

297366
<br><br><!-- These go in the last form if there's more than one: -->

0 commit comments

Comments
 (0)