Jul 16, 2026
5 min read
A regular expression (regex) is a pattern that describes text: "an email address," "a date in YYYY-MM-DD format," "any line starting with ERROR." Once written, one regex can search, validate, or extract matching text across thousands of lines instantly.
Regex looks intimidating because it is dense, not because it is complex. ^\d{4}-\d{2}-\d{2}$ reads as gibberish until you know the vocabulary — then it reads as "start, four digits, dash, two digits, dash, two digits, end." A date.
This guide teaches that vocabulary in the order you will actually use it. Open the free Regex Tester in another tab and try each pattern as you go — regex is learned by watching matches light up, not by reading.
The simplest regex is just text. The pattern cat matches "cat" anywhere: in "cat", "catalog", "concatenate". That last part surprises beginners — regex matches substrings by default. Controlling where a match may occur is what anchors are for (below).
Square brackets mean "exactly one character, from this set":
[aeiou] — one vowel[0-9] — one digit (ranges work with a dash)[a-zA-Z] — one letter, either case[^0-9] — the ^ inside brackets negates: one character that is not a digitThe common sets have shortcuts you will use constantly:
| Shorthand | Means | Equivalent |
|---|---|---|
\d | digit | [0-9] |
\w | word character | [a-zA-Z0-9_] |
\s | whitespace | space, tab, newline |
. | any character except newline | — |
\D \W \S | negations of the above | — |
Because . means "anything," matching a literal dot requires escaping: \.. Forgetting this is the classic beginner bug — 1.99 as a pattern happily matches "1X99".
A quantifier applies to whatever is immediately before it:
* — zero or more: ab*c matches "ac", "abc", "abbbc"+ — one or more: \d+ matches "7", "2026", but not ""? — zero or one (optional): colou?r matches "color" and "colour"{4} — exactly four: \d{4} matches a year{2,5} — two to five; {2,} — two or moreWith just classes and quantifiers you can already write useful patterns. A YYYY-MM-DD date: \d{4}-\d{2}-\d{2}.
Anchors match positions, not characters:
^ — start of the string (or line)$ — end of the string (or line)\b — word boundaryThis is how you fix the "cat in concatenate" problem: \bcat\b matches "cat" as a whole word only. And it is the difference between finding a date and validating one: ^\d{4}-\d{2}-\d{2}$ accepts "2026-07-16" but rejects "x2026-07-16y", because the pattern must span the entire string.
Parentheses group parts of a pattern, and | means "or":
(cat|dog) — matches "cat" or "dog"(ab)+ — one or more repetitions of "ab": "ab", "abab"(\d{4})-(\d{2})-(\d{2}), the year, month, and day are extracted as groups 1, 2, and 3 — this is how you pull data out of text, not just find it.A simple email check — ^[\w.+-]+@[\w-]+\.[\w.]+$
One or more word/dot/plus/dash characters, an @, a domain, a literal dot, and a top-level domain. (Real-world email validation is famously messy; for coursework, this level is what is expected.)
Extract hashtags — #\w+
A literal # followed by one or more word characters. Run it globally over a post and every hashtag pops out.
Find ERROR lines in a log — ^ERROR\b.*$
Line starts with the word ERROR, then anything to the end of the line.
Paste any of these into the Regex Tester with your own sample text and watch which parts match — then break them on purpose. Changing + to *, or removing an anchor, and predicting the result is the fastest way to make the vocabulary stick.
. ? + ( ) [ ] all need \ when you mean them literally.<.+> swallows <b>bold</b> entirely; the lazy version <.+?> stops at the first >.^...$, a "valid" check passes if the pattern appears anywhere in the input.Regex appears in every language you will use — Python's re, JavaScript's /pattern/, Java's Pattern, SQL's REGEXP, and every text editor's find-and-replace. Learn it once, use it for a career.
If a regex-heavy assignment has you stuck — or the assignment is due and the pattern still will not match — the programming help service at EduSupport can pair you with a developer who will untangle it with you, step by step.
Our expert team is available 24/7 for assignments, projects, and exam prep.