Regex Syntax Flashcards
What doesn’t this match /./?
newline characters
what does the g flag do?
searches all instances
what does the i flag do?
ignores upper and lower case
How do you overcome special symbols like ?
backslash to escape
\
How to match one of several patterns?
/cat/ and or /dog/
cat|dog|rabbit
pipe operator and brackets
Write a regex that matches blueberry or blackberry, but write berry precisely once. Test it with these strings:
blueberry
blackberry
black berry
strawberry
Why does this work?
(blue|black)berry
concatenation works with patterns, not characters. Thus, we can concatenate (blue|black) with berry to produce the final result.
Write regex to match all the as, bs or cs in: (2 ways)
Four score + seven
(a|b|c)
[abc]
make a regex that will match:
Hoover
hoover
but not match
HooveR
hooveR
(i flag won’t work here)
[hH]hoover
Write a regex to match all of the following
a1
a2
a3
b1
b2
b3
[ab][123]
how about all the combinations from a-f and 1-9
[a-f][1-9]
Match a single character of characters from a-f or x-z
[a-fx-z]
Why should you never do this: [A-z]
because there are symbols for other characters in between.
Do this instead:
[A-Za-z]
Match all characters EXCEPT the y, a or t
[^yat]
Using square brackets, make a regex that will match:
cat, cot, cut, bat, bot, or but
[bc][aou]t
Base 20 digits include the decimal digits 0 through 9, and the letters A through J in upper or lowercase. Write a regex that matches base 20 digits. Test it with these strings:
0xDEADBEEF
1234.5678
Jamaica
plow ahead
[0-9a-j]
Write a regex that matches any LETTER (not special characters) except x or X. (1 ways) Test it with these strings:
0x1234
Too many XXXXXXXXXXxxxxxxXXXXXXXXXXXX to count.
The quick brown fox jumps over the lazy dog
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
[A-WYZa-wyz]
Match any character that is not a letter (with and without i):
[^a-z]i
[^a-zA-Z]
Are /(ABC|abc)/ and /[Aa][Bb][Cc]/ equivalent regex?
no
Are /abc/i and /[Aa][Bb][Cc]/ equivalent regex?
yes
write a regex that matches a string that looks like a simple negated character class range, e.g., ‘[^a-z]’. (Your answer should match precisely six characters. The match does not include the slash characters.) Test it with these strings:
The regex /[^a-z]/i matches any character that is
not a letter. Similarly, /[^0-9]/ matches any
non-digit while /[^A-Z]/ matches any character
that is not an uppercase letter. Beware: /[^+-<]/
is at best obscure, and may even be wrong.
[\^[A-Za-z0-9]-[A-Za-z0-9]]
What is a character class pattern?
Character class patterns use a list of characters between square brackets, e.g., /[abc]/. Such a pattern matches a single occurrence of any of the characters between the brackets. Try these regex:
Match any character (except newline characters)
/./
Match any whitespace character (using class shortcut):
space (‘ ‘), tab (‘\t’), vertical tab (‘\v’), carriage return (‘\r’), line feed (‘\n’), and form feed (‘\f’),
Match any non-whitespace character (the others)
/s
/S
match numbers 0-9 (using class shortcut)
match any non-digit character
(using class shortcut)
\d
\D
Match word characters a-z i 0-9
Match nonword characters
\w
\W
Write a regex that matches any sequence of three characters delimited by whitespace characters (the regex should match both the delimiting whitespace and the sequence of 3 characters). Test it with these strings:
reds and blues
the lazy cat sleeps
\s…\s
\s…\s
on
Doc in a big red box.
Hup! 2 3 4
will not match red. Why not?
doesn’t match since the regex engine consumes the space character that precedes red when it matches big (note the trailing space). Once consumed as part of a match, the character is no longer available for subsequent matches.