Regex Syntax Flashcards

1
Q

What doesn’t this match /./?

A

newline characters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what does the g flag do?

A

searches all instances

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what does the i flag do?

A

ignores upper and lower case

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you overcome special symbols like ?

A

backslash to escape
\

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to match one of several patterns?
/cat/ and or /dog/

A

cat|dog|rabbit

pipe operator and brackets

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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?

A

(blue|black)berry

concatenation works with patterns, not characters. Thus, we can concatenate (blue|black) with berry to produce the final result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write regex to match all the as, bs or cs in: (2 ways)
Four score + seven

A

(a|b|c)
[abc]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

make a regex that will match:
Hoover
hoover
but not match
HooveR
hooveR
(i flag won’t work here)

A

[hH]hoover

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write a regex to match all of the following
a1
a2
a3
b1
b2
b3

A

[ab][123]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how about all the combinations from a-f and 1-9

A

[a-f][1-9]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Match a single character of characters from a-f or x-z

A

[a-fx-z]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why should you never do this: [A-z]

A

because there are symbols for other characters in between.
Do this instead:
[A-Za-z]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Match all characters EXCEPT the y, a or t

A

[^yat]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Using square brackets, make a regex that will match:
cat, cot, cut, bat, bot, or but

A

[bc][aou]t

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

[0-9a-j]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

[A-WYZa-wyz]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Match any character that is not a letter (with and without i):

A

[^a-z]i
[^a-zA-Z]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Are /(ABC|abc)/ and /[Aa][Bb][Cc]/ equivalent regex?

A

no

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Are /abc/i and /[Aa][Bb][Cc]/ equivalent regex?

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

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

[\^[A-Za-z0-9]-[A-Za-z0-9]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is a character class pattern?

A

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:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Match any character (except newline characters)

A

/./

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

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)

A

/s
/S

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

match numbers 0-9 (using class shortcut)

match any non-digit character
(using class shortcut)

A

\d

\D

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Match word characters a-z i 0-9

Match nonword characters

A

\w
\W

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

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

A

\s…\s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

\s…\s
on
Doc in a big red box.
Hup! 2 3 4

will not match red. Why not?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Write a regex that matches any four digit hexadecimal number that is both preceded and followed by whitespace. Note that 0x1234 is not a hexadecimal number in this exercise: there is no space before the number 1234.

Hello 4567 bye CDEF - cdef
0x1234 0x5678 0xABCD
1F8A done

2 matches

A

/\s[\dA-F][\dA-F][\dA-F][\dA-F]\s/ig

29
Q

Write a regex that matches any sequence of three letters. Test it with these strings:

A

(there are no class shortcuts for just letters)
/[a-z][a-z][a-z]/i

30
Q

Write regex to target ‘cat’ at the beginning, end of a string, or beginning and end of a string
cat blah blah
blah blah cat

A

^cat
cat$
^cat$

31
Q

Write regex to match 3 characterse of strings or numbers
using word bound anchors

One fish,
Two fish,
Red fish,
Blue fish.
123 456 7890

A

\b\w\w\w\b

32
Q

Match the first 3 characters of a word
Match the last 3

A

/\b\w\w\w
\w\w\w\b/

33
Q

match the john in
abcjohnabc
doejohn
but not the john in
johnson

A

\Bjohn

34
Q

Write a regex that matches the word The when it occurs at the beginning of a line. Test it with these strings:
The lazy cat sleeps.
The number 623 is not a word.
Then, we went to the movies.
Ah. The bus has arrived.

A

^the\b
need the \b to make sure it’s just the word the and not then

35
Q

Write a regex to get cat in:
The number 623 is not a cat
but not:
The lazy cat sleeps
The Alaskan drives a snowcat

A

\bcat$
need the \b to make sure it’s just the word cat and not scat or snowcat

36
Q

Write a regex that matches any three-letter word; a word is any string comprised entirely of letters. You can use these test strings.
reds and blues
The lazy cat sleeps.
The number 623 is not a word. Or is it?

A

b[a-z][a-z][a-z]\b

37
Q

Write a regex that matches an entire line of text that consists of exactly 3 words as follows:

The first word is A or The.
There is a single space between the first and second words.
The second word is any 4-letter word.
There is a single space between the second and third words.
The third word – the last word – is either dog or cat.
Test your solution with these strings:

A grey cat
A blue caterpillar
The lazy dog
The white cat
A loud dog
–A loud dog
Go away dog
The ugly rat
The lazy, loud dog

A

(^(a|the) [a-z][a-z][a-z][a-z] (dog|cat)$)

38
Q

Write a regex that matches an entire line of text that consists of exactly 3 words as follows:

The first word is A or The.
There is a single space between the first and second words.
The second word is any 4-letter word.
There is a single space between the second and third words.
The third word – the last word – is either dog or cat.
Test your solution with these strings:

A grey cat
A blue caterpillar
The lazy dog
The white cat
A loud dog
–A loud dog
Go away dog
The ugly rat
The lazy, loud dog

A

(^(a|the) [a-z][a-z][a-z][a-z] (dog|cat)$)

39
Q

What’s the difference between a non-whitespace character and a word character

A

non-whitespace character includes symbols
word character is just a-z 0-9

40
Q

Write a regex to match 3 or more digits limited as a word

A

\b/d/d/d/d\b
the /d
doesn’t mean 4 digits, it means “more digits if needed” “Zero or more additional digits”

41
Q

what happens if you have x* with no words bounds?

A

Because it matches “0 or more of x” it will max 0. Like empty strings.

42
Q

What will this match /co*t/?
ct
cot
coot
cooot

A

All

43
Q

Which of these will this match?
\b\d\d\d\d+
123
1234
12345
12
1

A

4 or more

43
Q

What will this match:
1(234)*5
of these?
15
12345
12342342345
1234235

A

Top 3

44
Q

So what does \d+ match? compared to \d*

A

1 or more
0 or more

45
Q

Which of these will this match? coo?t
ct
cot
coot
cooot

A

cot or coot

46
Q

what does /x?/ match

A

either x or nothingness

47
Q

Write a regex to match 20170111 or 2017-01-11

A

\b\d\d\d\d-?\d\d-?\d\d\b

48
Q

Write a regex to match 20170111 or 2017-01-11 or 2015/02/12

A

The answer given was \d\d\d\d(-?|/?)\d\d(-?|/?)\d\d
I think (-|/)? is better. Because (-?|/) would do it. - or ‘’, or /

49
Q

will a? match an empty string like a*?

A

yes

50
Q

What will this match: asd{2}
of
asdasdasdasd

A

Nothing
it’s looking for as and then 2 ds
asdd

51
Q

write a regex to match 10 digits (but not 9 or 11)

A

\d{10}

52
Q

write a regex to match 10 or more digits

A

\d{10,}

53
Q

write a regex to match 10 to 13 digits

A

\d{10,13}

54
Q

write a regex to match 5-8 letter words

A

\b[a-zA-Z]{5,8}\b

55
Q

What will this match:
a[abc]*c
of this:
xabcbcbacy

A

abcbcbac
The longest thing it can. It’s greedy.

56
Q

Make a[abc]*c
Not match the longest thing it can

A

a[abc]*?c
Question mark on the main quantifier

57
Q

Write a regex that matches any word that begins with b and ends with an e, and has any number of letters in-between. You may limit your regex to lowercase letters. Test it with these strings.

To be or not to be
Be a busy bee
I brake for animals.

A

\bb[a-z]*e\b

58
Q

Write a regex that matches any LINE (the whole line, the whole line, the whole line) of text that ends with a ?. Test it with these strings.
Then make one that doesn’t match a line that is only a ?

What’s up, doc?
Say what? No way.
?
Who? What? Where? When? How?

A

^.*\?$

Then
^.{1,}\?$
or
^.+\?$

59
Q

Write a regex that matches any line of text that contains nothing but a URL. For this exercise, a URL begins with http:// or https://, and continues until it detects a whitespace character or end of line. Test your regex with these strings:

http://launchschool.com/
https://mail.google.com/mail/u/0/#inbox
htpps://example.com
Go to http://launchschool.com/
https://user.example.com/test.cgi?a=p&c=0&t=0&g=0 hello
http://launchschool.com/

Then modify to allow whitespace before and after.
Then so that it can appear anywhere (as long as it’s word bound)

A

^https?:\/\/\S*$

^https?:\/\/\S*$

\bhttps?:\/\/\S*\b

60
Q

Write a regex that matches any word that contains at least three occurrences of the letter i. Test your regex against these strings:

Mississippi
ziti 0minimize7
inviting illegal iridium

A

\b([a-z]i){3,}[a-z]\b
gi

61
Q

Write a regex that matches the last word in each line of text. For this exercise, assume that words are any sequence of non-whitespace characters. Test your regex against these strings:

What’s up, doc?
I tawt I taw a putty tat!
Thufferin’ thuccotath!
Oh my darling, Clementine!
Camptown ladies sing this song, doo dah.

A

\b\S+$

62
Q

Write a regex that matches lines of text that contain at least 3, but no more than 6, consecutive comma separated numbers. You may assume that every number on each line is both preceded by and followed by a comma. Test your regex against these strings:

,123,456,789,123,345,
,123,456,,789,123,
,23,56,7,
,13,45,78,23,45,34,
,13,45,78,23,45,34,56,

A

^,(\d+,){3,6}$

63
Q

Write a regex that matches lines of text that contain at least 3, but no more than 6, consecutive comma separated numbers. In this exercise, you can assume that the first number on each line is not preceded by a comma, and the last number is not followed by a comma. Test your regex against these strings:

123,456,789,123,345
123,456,,789,123
23,56,7
13,45,78,23,45,34
13,45,78,23,45,34,56

A

/^(\d+,){2,5}\d+$/

64
Q

Write a regex that matches lines of text that contain either 3 comma separated numbers or 6 or more comma separated numbers. Test your regex against these strings: ( 2 ways, 1 version without pipe operator)

123,456,789,123,345
123,456,,789,123
23,56,7
13,45,78,23,45,34
13,45,78,23,45,34,56

A

(^(\d+,){2}\d+$|^(\d+,){5,}\d+$)
or
^(\d+,){2}((\d+,){3,})?\d+$

65
Q

Write a regex that matches HTML h1 header tags, e.g.,

<h1>Main Heading</h1>

<h1>Another Main Heading</h1>

<h1>ABC</h1>

<p>Paragraph</p>

<h1>DEF</h1>

<p>Done</p>

(Copy it from here:)
https://launchschool.com/books/regex/read/quantifiers#greediness

A

<h1>.*?</h1>

66
Q

Broad category name for these thigns:
+*?{}

A

quantifier

67
Q

what will this match:
[a-z]*i
of
aiz
iz
aaaizzzi

A

ai
i
aaaizzzi