Regex for Beginners: How to Write Regular Expressions
Regex for Beginners
Regular expressions — regex — are patterns that describe text. With regex, you can search strings, validate input, extract data, and perform complex find-and-replace using a single compact expression. Once you understand the fundamentals, regex becomes one of the most powerful tools in your programming toolkit.
What Is a Regular Expression?
A regex is a sequence of characters defining a search pattern. Pattern \d{3}-\d{2}-\d{4} matches a US Social Security number: three digits, hyphen, two digits, hyphen, four digits.
Regex engines exist in virtually every programming language — JavaScript, Python, Java, Ruby, Go — all with built-in support.
Literal Characters
Most characters match themselves. The pattern cat matches the literal string "cat" wherever it appears.
Pattern: cat
Matches: "cat" in "The cat sat on the mat."
Matches: "cat" in "catalog"
Literal matching is straightforward but limited. The real power comes from metacharacters.
Metacharacters
These characters have special meaning and must be escaped with \ to match literally:
. ^ $ * + ? { } [ ] \ | ( )
Character Classes
Character classes match one character from a set.
Square Bracket Notation
| Pattern | Matches |
|---------|---------|
| [aeiou] | Any vowel |
| [0-9] | Any digit |
| [a-zA-Z] | Any letter |
| [^0-9] | Any character that is NOT a digit |
Predefined Classes
| Shorthand | Description |
|-----------|-------------|
| \d | Any digit (same as [0-9]) |
| \w | Letter, digit, or underscore |
| \s | Space, tab, newline |
| \D | Any non-digit |
| \W | Any non-word character |
| \S | Any non-whitespace |
| . | Any character except newline |
Quantifiers
Quantifiers control how many times the preceding element must appear.
| Quantifier | Meaning | Example | Matches |
|------------|---------|---------|---------|
| * | Zero or more | a* | "", "a", "aa" |
| + | One or more | a+ | "a", "aa", "aaa" |
| ? | Zero or one | colou?r | "color", "colour" |
| {3} | Exactly N | \d{3} | "123" |
| {2,4} | Between N and M | \d{2,4} | "12", "123", "1234" |
Examples
\d+ → "42" in "Answer is 42"
https?:// → "http://" and "https://"
Anchors
Anchors match positions, not characters.
| Anchor | Meaning |
|--------|---------|
| ^ | Start of string |
| $ | End of string |
| \b | Word boundary |
| \B | Non-word boundary |
^hello → matches "hello" only at the start
end$ → matches "end" only at the end
\bword\b → matches "word" as a whole word, not inside "sword"
Groups and Alternation
Capturing groups ( ) extract matched portions:
(\d{3})-(\d{4}) → "555-1234", captures "555" in group 1, "1234" in group 2
Non-capturing groups (?: ) group without extracting:
(?:https?|ftp):// → "http://", "https://", "ftp://"
Alternation | acts as OR:
cat|dog|bird → matches "cat", "dog", or "bird"
Practical Regex Examples
| Use Case | Pattern |
|----------|---------|
| US phone | \d{3}-\d{3}-\d{4} |
| Email (simple) | \S+@\S+\.\S+ |
| IP address | \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} |
| Hashtag | #[a-zA-Z]\w* |
| Time (HH:MM) | [01]\d|2[0-3]:[0-5]\d |
How to Test a Regex Pattern
Always test patterns interactively:
- Enter your regex pattern.
- Enter a test string.
- See which portions match in real time.
- Adjust and repeat.
Use the regex tester tool to experiment with patterns. It highlights matches, shows capture groups, and helps you understand exactly what each part of your expression does.