Regex Basics

Regular expression fundamentals

Literal Characters

abc # matches abc
123 # matches 123

Metacharacters

. # any single character (except newline)
\ # escape special character
| # alternation (OR)

Character Classes

[abc] # matches a, b, or c
[^abc] # matches anything except a, b, c
[a-z] # matches lowercase letters
[A-Z] # matches uppercase letters
[0-9] # matches digits
[a-zA-Z] # matches any letter

Predefined Classes

\d # digit [0-9]
\D # non-digit
\w # word character [a-zA-Z0-9_]
\W # non-word character
\s # whitespace [ \t\n\r\f]
\S # non-whitespace