Regular Expressions



• allow you to specify a pattern of text to search for

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Character Class



# Character Class Shorthand
\d          # any numeric digit from 0 to 9
            # or (0|1|2|3|4|5|6|7|8|9)
\D          # any character that is NOT a numeric digit

\w          # any letter, numeric digit, or the underscore character
\W          # any character that is NOT a letter, numeric digit, or 
            # the underscore character

\s          # any space, tab, or newline character
\S          # any character that is not a space, tab, or newline

# Custom Character Class
[aeiouAEIOU]    # vowel
[a-zA-Z0-9]     # alpha-numeric
[0-5.]          # match digits 0 to 5 and a period
                # (no escape characters required)


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Matching Specific Repetitions with Curly Brackets {}



# regex

\d{3}       # match 3 times \d\d\d

?           # match zero or one of the group preceding ?
            # non greedy match (match the shortest string possible)
*           # match zero or more of the preceding character
+           # match one or more of the preceding character
{}          # match specific repetitions
(Ha){3,}    # match 3 or more instanaces of (Ha) group
(Ha){,5}    # match 0 to 5 instances
{Ha}{3,5}?  # non greedy match


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The Caret (^) and Dollar Sign ($) Characters


• “Carrots cost dollars” - ^ comes first and $ comes last

^<regex>    # match anything that begins with <regex>
<regex>$    # match anything that ends with <regex>
^<regex>$   # exact match

#Examples:
\d$
^\d+$


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The Wildcard Character - Dot (.)



.           # match any single character except the newline

# Example:
'.at'       # matches: cat hat sat lat mat


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Matching Everything with Dot-Star (.*)



# Example
# Match the string 'First Name:', followed by any and all text, followed by 'Last Name:', and then followed by anything again
'First Name: (.*) Last: Name: (.*)'


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Online Tools



https://regexr.com/




Index