How to use the cheat sheet
- Type what you are looking for, such as
digit,optionaloremail. - Click a pattern to copy it.
- Paste it into your code or try it in the regex tester.
How to read a regular expression
A regular expression is a search pattern. Most characters match themselves, so cat finds the letters c, a, t in a row. A few characters have special meaning: . matches any character, * and + repeat the thing before them, and square brackets list the characters allowed in one position. Read a pattern from left to right, one piece at a time.
For example, ^\d{4}-\d{2}-\d{2}$ reads as: start of text, four digits, a hyphen, two digits, a hyphen, two digits, end of text. That matches a date such as 2026-01-31 and nothing else.
Greedy and lazy matching
Quantifiers are greedy: they take as much text as they can. On <b>one</b> and <b>two</b>, the pattern <b>.*</b> matches the whole line. Add a question mark to make it lazy, <b>.*?</b>, and it stops at the first closing tag.
Characters you need to escape
To match one of these as plain text, put a backslash in front of it: . * + ? ^ $ ( ) [ ] { } | \ /. Inside square brackets only ], \, ^ and - need care.
Differences between languages
This sheet follows JavaScript, and nearly all of it works the same in Python, Java, C#, Go, PHP and Rust. The main differences: Go and Rust have no lookarounds or backreferences, Python writes named groups as (?P<name>...), and tools such as grep need the -E or -P option for the modern syntax.
Test a pattern against your own text in the regex tester. Working in the terminal? See the Git cheat sheet.
Frequently asked questions
What does \d mean in regex?
It matches one digit from 0 to 9. Add a plus sign, \d+, to match one or more digits in a row.
What is the difference between * and +?
The star matches zero or more of the item before it, so the item can be missing. The plus needs at least one.
How do I match a literal dot or question mark?
Put a backslash before it: \. matches a dot and \? matches a question mark.
Is there a perfect regex for email addresses?
No. The simple pattern on this page catches typing mistakes, which is what most forms need. The only real test of an address is sending mail to it.
Do these patterns work in Python and other languages?
Yes, with small exceptions that are listed in the section on differences between languages.