Regex for Beginners: Five Patterns That Solve 90% of Everyday Tasks
The task: pull every date out of a document, find every line with prices in a log, check that the email in a form looks like an email. The solution is always the same — a regular expression. It looks like a spell made of dots, asterisks, and brackets, but it's simpler than it seems: just a pattern describing what the piece of text you need should look like.
What a pattern is made of
- ordinary characters stand for themselves: the word "cat" matches the word "cat";
- \d is any digit, \w is a letter, digit, or underscore, \s is a space;
- ^ and $ are the start and end of a line;
- quantifiers: * (zero or more), + (one or more), ? (optional), {2,4} (two to four times);
- a dot matches any character, which is why a real dot is written \.
Let's assemble this into a live example. A date like 04.09.2026 is two digits, a dot, two digits, a dot, four digits. The pattern: \d{2}\.\d{2}\.\d{4}. That's it — no "spells": in plain English it reads as "two digits, dot, two digits, dot, four digits."
Five everyday patterns
- date — \d{2}\.\d{2}\.\d{4} (04.09.2026);
- time — \d{2}:\d{2} (14:37);
- email (simplified, but enough for searching through text) — \S+@\S+\.\S+;
- price — \d+(?:[ ,]\d+)?\s?(usd|\$|eur) — catches both "1500 usd" and "1,500 eur";
- double spaces — " " (two spaces) replaced with one: the go-to pattern for cleaning up text copied from a PDF.
How to test without breaking anything
The main habit when working with regex is to test against real text first. Open the regex tester, paste a chunk of the actual document and your pattern: every match gets highlighted, and you can instantly see where the regex gets greedy and grabs too much. The beginner trap is right here: a \d+ pattern meant for "prices" will also collect the digits from phone numbers, if the text has any. A test on live data shows that in a second.
Once the pattern is debugged, you can carry it into find and replace — the same syntax is supported there, including substitutions with capture groups. That's how people clean up lists, rename fields in exports, and extract values from logs.
When regex isn't the answer
Regular expressions are excellent at searching "flat" text and poor at nested structure. Parsing HTML with regex is the classic mistake: nested tags can't formally be described by a regular language, and sooner or later the pattern breaks on some innocent fragment. For structured data, use parsers, and leave regex to what it was built for — search and replace in text.
Regex Tester & Debugger
Test regular expressions with match highlighting, groups, and common patterns
Find & Replace
Find and replace text with support for case sensitivity, whole word matching, and regular expressions
Frequently asked questions
What does a dot mean in a regular expression?
A dot matches any single character except a line break. To search for a literal dot, escape it: .
What do the ^ and $ symbols mean?
^ marks the start of a line, $ marks the end. The pattern ^abc matches abc only at the beginning, abc$ only at the end.
How do I make part of a pattern optional?
Put a question mark after it: colou?r matches both color and colour — the letter u becomes optional.