Sort-O-Matic
Location: Workshop
Introduction
Minty Candycane tells us about this challenge
Hey there, KringleCon attendee! I'm Minty Candycane!
I'm working on fixing the Present Sort-O-Matic.
The Sort-O-Matic uses JavaScript regular expressions to sort presents apart from misfit toys, but it's not working right.
With some tools, regexes need / at the beginning and the ends, but they aren't used here.
You can find a regular expression cheat sheet here if you need it.
You can use this regex interpreter to test your regex against the required Sort-O-Matic patterns.
Do you think you can help me fix it?
...
Hints
Solution
1. Matches at least one digit
Create a regular expression that will only match any string containing at least one digit.
Answer: \d+
\d
matches a digit
+
means match the preceding character one or more times.
2. Matches 3 alpha a-z characters ignoring case
Create a regular expression that will only match only alpha characters A-Z of at least 3 characters in length or greater while ignoring case.
Answer: [a-zA-Z]{3}
[]
indicates a class or collection of character any of which can be matched
{x}
indicates the number of times, x
, a consecutive match must occur
3. Matches 2 chars of lowercase a-z or numbers
Create a regular expression that will only match at least two consecutive lowercase a-z or numeric characters.
Answer: [\da-z]{2}
4. Matches any 2 chars not uppercase A-L or 1-5
Create a regular expression that will only match any two characters that are NOT uppercase A through L and NOT numbers 1 through 5.
Answer: [^A-L1-5]{2}
[^A-L1-5]
- the ^
negates that match
5. Matches three or more digits only
Create a regular expression that only matches if the entire string is composed of entirely digits and is at least 3 characters in length.
Answer: ^\d{3,}$
{x,}
- indicates that the match must occur x
or more times
6. Matches multiple hour:minute:second time formats only
Create a regular expression that only matches if the entire string is a valid Hour, Minute and Seconds time format similar to the following:
12:24:53
1:05:24
23:02:43
08:04:10
However, the following would be invalid:
25:30:86
A1:E4:B5
B2:13:4A
32:24:53
08:74:53
12:5:24
Use anchors or boundary markers to avoid matching other surrounding strings.
Answer: ^[0-2]{0,1}[0-9]{1}:[0-5][0-9]:[0-5][0-9]$
^
and $
as used above indicate the beginning and end of the line, so the whole line must match the pattern
{x,y}
- indicates that the match must occur between x
and y
times
7. Matches MAC address format only while ignoring case
Create a regular expression that only matches if the entire string is a MAC address. For example:
00:0a:95:9d:68:16
76:A4:5A:D2:69:93
B8:13:13:D1:18:EC
95:ce:00:4a:22:df
However, the following would be examples of invalid MAC Addresses:
97:z2:gf:c4:02:c2
de:140:130:69:7_-bd
C0:HH:EE:50:B7:C3
Use anchors or boundary markers to avoid matching other surrounding strings.
Answer: ^[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}$
No new syntax
8. Matches multiple day, month, and year date formats only
Create a regular expression that only matches one of the three following day, month, and four digit year formats:
10/01/1978
01.10.1987
14-12-1991
However, the following values would be invalid formats:
05/25/89
12-32-1989
01.1.1989
1/1/1
Use anchors or boundary markers to avoid matching other surrounding strings.
Answer: ^([012][1-9]|10|20|30|31)[.\/-]([012][1-9]|10|20|30|31)[.\/-][0-9]{4}$
()
matches the contents of the brackets
[012][1-9]|10|20|30|31
will match the 2 digit numbers 01 to 31.
|
indicates an OR match
\
is used to escape special characters