REGEX Flashcards
How do you specify a unique character?
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’.
What if you wanted a or b or c, for a specific character index?
bracket notation [ ]
[abc] would match only “a” or “b” or “c”.
What does \d match?
\d matches to ANY digit.
What does \D match?
\D matches with any NON-digit.
What is the REGEX notation for any character?
the period . is the marker for a wild card in regex
to notate a period it must be .
What does \w match?
\w matches to any Alphanumeric character.
What does \W match?
\W matches to any NON-alphanumeric character
How do you express not “x”, nor “y”, nor “z” ?
[^xyz]
the index cannot have x nor y nor z.
How would you match a range of characters?
With a dash -
for example to match capitalized names:
[A-Z][a-z] would match an uppercase character followed by a lowercase character.
How do you specify repetitions?
with curly brackets and a comma.
{ , }
z{3, 5} would match zzz, zzzz, or zzzzz.
How can you express zero or more repetition?
with the * character
aa* would match “”, “aa”, or “aaaa”.
How can you express one or more repetitions?
with the + character
bb+ would match “bb”, “bbbbbb”, but not “”.
How can you denote a character is option?
Follow the character with a ‘?’ to denote it is optional.
ab?c can match ac and abc.
How can you match any whitespace?
with the \s expression.
What does \S match to?
Any NON-whitespace
How do you match a specific string?
within a ^ and $
^my string$ will only match “my string”.
What are ^ … $
Outside of [] the ^ represents ‘starts with’
while $ represents ‘ends with’
for situations you have
^start\regex\end$
or
^startend$
What do parenthesis do ( )
They create a ‘capture group’ to represent a sub-pattern within the REGEX
How can you express ‘or’ withing a REGEX
With | (cat|dog)s
would be “cats” or “dogs”
What does \b represent?
“Boundary Anchor”
It’s a zero-width assertion and does not consume any characters in the string.
What are the four use cases for a Boundary Anchor
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.