REGEX Flashcards

1
Q

How do you specify a unique character?

A

The simplest way is to add in the character itself to the REGEX.
i.e. a for ‘a’, D for ‘D’, and 3 for ‘3’.

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

What if you wanted a or b or c, for a specific character index?

A

bracket notation [ ]
[abc] would match only “a” or “b” or “c”.

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

What does \d match?

A

\d matches to ANY digit.

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

What does \D match?

A

\D matches with any NON-digit.

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

What is the REGEX notation for any character?

A

the period . is the marker for a wild card in regex

to notate a period it must be .

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

What does \w match?

A

\w matches to any Alphanumeric character.

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

What does \W match?

A

\W matches to any NON-alphanumeric character

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

How do you express not “x”, nor “y”, nor “z” ?

A

[^xyz]
the index cannot have x nor y nor z.

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

How would you match a range of characters?

A

With a dash -

for example to match capitalized names:
[A-Z][a-z] would match an uppercase character followed by a lowercase character.

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

How do you specify repetitions?

A

with curly brackets and a comma.
{ , }

z{3, 5} would match zzz, zzzz, or zzzzz.

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

How can you express zero or more repetition?

A

with the * character
aa* would match “”, “aa”, or “aaaa”.

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

How can you express one or more repetitions?

A

with the + character
bb+ would match “bb”, “bbbbbb”, but not “”.

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

How can you denote a character is option?

A

Follow the character with a ‘?’ to denote it is optional.

ab?c can match ac and abc.

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

How can you match any whitespace?

A

with the \s expression.

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

What does \S match to?

A

Any NON-whitespace

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

How do you match a specific string?

A

within a ^ and $
^my string$ will only match “my string”.

17
Q

What are ^ … $

A

Outside of [] the ^ represents ‘starts with’

while $ represents ‘ends with’

for situations you have
^start\regex\end$
or
^startend$

18
Q

What do parenthesis do ( )

A

They create a ‘capture group’ to represent a sub-pattern within the REGEX

19
Q

How can you express ‘or’ withing a REGEX

A

With | (cat|dog)s
would be “cats” or “dogs”

20
Q

What does \b represent?

A

“Boundary Anchor”

It’s a zero-width assertion and does not consume any characters in the string.

21
Q

What are the four use cases for a Boundary Anchor

A

1) Word Boundary at the beginning or end ::
\btalk
matches “talk” at the beginning of the index.
talk\b
matches “talk” from the end of talk.

2) Whole word matching
\bMyWord\b

3) Excluding substrings
\bcat\b
matches “cat” but not concatenate.

4) Word Boundary in the Middle:

\bword1\b.*\bword2\b matches a pattern where “word1” and “word2” are whole words separated by any characters.