Catalog / Regular Expressions (Regex) Examples Cheatsheet
Regular Expressions (Regex) Examples Cheatsheet
A comprehensive cheat sheet providing practical examples of regular expressions for various use cases. Master regex with these examples!
Basic Matching
Literal Matching
Pattern: |
|
Matches: |
The literal string “hello”. |
Example: |
|
Pattern: |
|
Matches: |
The literal string “world”. |
Example: |
|
Character Classes
Pattern: |
|
Matches: |
Any single vowel (a, e, i, o, or u). |
Example: |
|
Pattern: |
|
Matches: |
Any single digit (0 to 9). |
Example: |
|
Anchors
Pattern: |
|
Matches: |
“hello” only at the beginning of the string. |
Example: |
|
Pattern: |
|
Matches: |
“world” only at the end of the string. |
Example: |
|
Quantifiers
Asterisk (*)
Pattern: |
|
Matches: |
Zero or more occurrences of “a”. |
Example: |
|
Plus (+)
Pattern: |
|
Matches: |
One or more occurrences of “a”. |
Example: |
|
Question Mark (?)
Pattern: |
|
Matches: |
Zero or one occurrence of “a”. |
Example: |
|
Curly Braces ({})
Pattern: |
|
Matches: |
Exactly three occurrences of “a”. |
Example: |
|
Pattern: |
|
Matches: |
Between two and four occurrences of “a”. |
Example: |
|
Special Characters and Escaping
Dot (.)
Pattern: |
|
Matches: |
“a”, followed by any character (except newline), followed by “b”. |
Example: |
|
Escaping Special Characters
Pattern: |
|
Matches: |
A literal dot character. |
Example: |
|
Pattern: |
|
Matches: |
A literal asterisk character. |
Example: |
|
Character Classes Shorthand
Pattern: |
|
Matches: |
Any digit (0-9). |
Example: |
|
Pattern: |
|
Matches: |
Any word character (a-z, A-Z, 0-9, and _). |
Example: |
|
Grouping and Capturing
Basic Grouping
Pattern: |
|
Matches: |
One or more occurrences of the group “abc”. |
Example: |
|
Capturing Groups
Pattern: |
|
Matches: |
A date format like “YYYY-MM-DD”. Captures the year, month, and day in separate groups. |
Example: |
|
Non-Capturing Groups
Pattern: |
|
Matches: |
One or more occurrences of the group “abc”, but without capturing the group. |
Example: |
|