The Basic Approach to Writing Regex
Writing a regex means using symbols to describe “what the text I want to match looks like”, assembled piece by piece from left to right: first think clearly about “what characters it’s made of, how many times each repeats, and whether the start and end need to be fixed.”
Step 1: Describe the Characters
- Write specific characters directly:
abc. - Use a character class for a category of characters: digits
\d, alphanumerics\w, or custom ones like[a-z],[aeiou]. - Use
.for any character (excludes newline by default).
Step 2: Describe the Quantity
Add a quantifier to a character to say how many times it repeats:
\d{4}— exactly 4 digits\w+— one or more alphanumerics-?— an optional hyphen\d{2,4}— 2 to 4 digits
Step 3: Groups and Alternation
- Use
( )to wrap the part you want to repeat as a whole or extract:(\d{4})-(\d{2})captures the year and month as two groups. - Named groups are more readable:
(?<year>\d{4}). - Use
|for “or”:(jpg|png|gif).
Step 4: Add Anchors to Fix Boundaries
^for start,$for end:^1[3-9]\d{9}$validates that the entire string is a phone number.\bfor word boundary:\bcat\bmatches only a standalone cat, not category.
Greedy vs. Non-Greedy (a Frequent Trap)
Quantifiers are greedy by default (match as much as possible). Against <a><b>, <.*> matches the whole thing; to match only <a>, use the non-greedy <.*?>. When you match “too much,” first consider whether you should add ?.
Using It in Code
The same regex is written slightly differently across languages:
- JavaScript:
/regex/flags, e.g.str.match(/\d+/g) - Python:
import re; re.findall(r'\d+', s)(use a raw stringr''to avoid backslash escaping) - Java:
Pattern.compile("\\d+")(backslashes must be doubled inside the string)
As you write, use the Regex Tester to see highlighting and groups in real time, and confirm it’s correct before pasting it into code.
Keep Learning
- Not familiar with the basic symbols: see What Is a Regular Expression;
- Want ready-made patterns: see Common Regular Expressions;
- Wrote one but it doesn’t match: see What to Do When a Regex Doesn’t Match.