Quick Answer

Regex (regular expressions) is a small pattern language for finding and matching text. You build patterns from a few pieces: literals, character classes, quantifiers, anchors, and groups. Learn those five building blocks, practice in a free online tester like regex101, and you can validate input, search code, and clean up data in almost any programming language.

Regex for Beginners: What Is a Regular Expression?

A regular expression (regex or regexp for short) is a compact way to describe a pattern in text. Instead of asking "does this string say cat?", regex lets you ask far richer questions: "does this look like an email?", "are there any 6-digit numbers here?", or "find every word that starts with a capital letter".

This guide to regex for beginners keeps things practical. You do not need to memorise everything. Regex is really just five small ideas stacked together: literals, character classes, quantifiers, anchors, and groups. Once those click, most patterns become readable.

Regex is built into almost every tool and language you already use, from your code editor's Find box to JavaScript, Python, Java, and databases. Learn it once and the same skill follows you everywhere.

Literals and Metacharacters

The simplest regex is just plain text, called a literal. The pattern cat matches the letters c-a-t wherever they appear, even inside a bigger word like "concatenate".

Some characters are not literal, though. They are metacharacters with a special job: . ^ $ * + ? ( ) [ ] { } | \. The most common one is the dot ., which means "any single character". If you want to match a real dot, you escape it with a backslash: \.

/cat/        matches "cat" in "concatenate"
/c.t/        . means any char: "cat", "cot", "cut"
/3\.14/      \. matches a literal dot: "3.14"

Rule of thumb: if a symbol has a special meaning and you want the plain character instead, put a backslash in front of it.

Character Classes: Matching a Set

A character class matches any one character from a set you list inside square brackets. [aeiou] matches a single vowel. You can use ranges with a hyphen, like [a-z] or [0-9], and combine them.

Put a caret ^ at the start of the brackets to negate it: [^0-9] means "any character that is not a digit".

Because some sets are so common, regex gives them shortcuts:

[aeiou]      any one vowel
[a-z0-9]     any lowercase letter or digit
[^0-9]       any character except a digit
\d           a digit, same as [0-9]
\w           a word char: letters, digits, underscore
\s           whitespace: space, tab, newline

Their capital versions mean the opposite: \D is a non-digit, \W a non-word character, and \S a non-space.

Quantifiers: How Many Times

So far each pattern matches one character. Quantifiers say how many times the thing before them should repeat.

  • * means zero or more
  • + means one or more
  • ? means zero or one (optional)
  • {n} means exactly n times, and {n,m} means between n and m times
a+           one or more "a": "a", "aaa"
\d{4}        exactly four digits: "2026"
\d{2,4}      two to four digits
colou?r      optional "u": "color" or "colour"
<.+>         greedy: grabs as much as possible
<.+?>        lazy: grabs as little as possible

By default quantifiers are greedy and match as much text as they can. Add a ? after them to make them lazy and stop at the first chance. This matters when you extract things like HTML tags, where greedy matching can accidentally swallow everything between the first and last tag.

Anchors: Where It Matches

Anchors do not match characters. They match positions. This lets you say "this must be at the start" or "this must be the whole thing".

  • ^ anchors to the start of the string (or line)
  • $ anchors to the end
  • \b is a word boundary, the edge between a word character and a non-word character
^Hello       matches "Hello" only at the start
world$       matches "world" only at the end
^\d{6}$      a string that is exactly six digits (a PIN code)
\bcat\b      "cat" as a whole word, not "category"

Anchors are the difference between validating and searching. Without ^ and $, the pattern \d{6} would happily match six digits sitting inside a longer string. With them, it insists the entire input is six digits and nothing else.

Groups, Alternation, and Capturing

Round brackets ( ) create a group. Groups let a quantifier apply to several characters at once, and they capture whatever matched so you can pull it out later.

Inside or across groups, the pipe | means OR (alternation): match this pattern or that one.

(ab)+             one or more "ab": "abab"
gr(a|e)y          "gray" or "grey"
(\d{4})-(\d{2})   two capture groups: year, then month
(?:...)           a group that does NOT capture

When you run a pattern like (\d{4})-(\d{2}) on "2026-07", the whole match is "2026-07", capture group 1 is "2026", and group 2 is "07". That is how you split a date, a phone number, or a log line into useful pieces. If you only need grouping and not the captured value, use a non-capturing group (?:...) to keep things tidy.

Practical Patterns: Email and Numbers

Time to combine the pieces. Here is a practical email check. Notice it is a mix of character classes, quantifiers, an escaped dot, and anchors:

// A practical (not perfect) email check
^[\w.+-]+@[\w-]+\.[\w.-]+$

// Extract every whole number from text
\d+

// Numbers with an optional minus and decimal part
-?\d+(\.\d+)?

Read the email pattern left to right: one or more word characters, dots, plus or minus ([\w.+-]+), then an @, then a domain, an escaped dot \., and an extension. The ^ and $ make sure the whole string fits.

An honest warning: there is no single "correct" email regex. The official specification is huge, and trying to catch every edge case makes the pattern unreadable. For real sign-up forms, use a simple pattern like the one above for a quick sanity check, then confirm the address by sending a verification email. That is more reliable than any regex.

Quick-Reference Table

Keep this handy while you practise. These are the pieces you will reach for again and again.

SymbolMeaningExample
.Any character except newlinea.c matches "abc"
\dAny digit\d\d matches "42"
\wWord character (letter, digit, _)\w+ matches "user_1"
\sWhitespacea\sb matches "a b"
*Zero or morelo*l matches "ll", "lool"
+One or more\d+ matches "2026"
?Optional (zero or one)colou?r
{n,m}Between n and m times\d{2,4}
[abc]Any one listed character[aeiou]
[^abc]Any character not listed[^0-9]
^ $Start / end of string^Hi, bye$
\bWord boundary\bcat\b
( )Group and capture(ab)+
|OR (alternation)cat|dog
\Escape a special char\. matches a real dot

Testing Tools and Next Steps

Never write regex blind. The fastest way to learn is an interactive tester that highlights matches as you type and explains each token.

  • regex101.com shows a live explanation, a match list, and captured groups. Pick your language flavour (JavaScript, Python, PHP, etc.) on the left.
  • regexr.com is clean and beginner-friendly with a built-in cheat sheet.
  • Your code editor (VS Code) supports regex in its Find and Replace box. Click the .* icon to turn it on.

Once a pattern works in a tester, use it in real code. In JavaScript, for example, /\d+/.test(str) checks for a number and str.match(/\d+/g) pulls all of them out. Learn how regex fits into functions, form validation, and data cleaning in our free JavaScript course.

Start small: match a word, then a digit, then a phone number. Build patterns one piece at a time and test after every change. That habit alone will save you hours.

Frequently Asked Questions

Is regex the same in every programming language?

The core building blocks, like \d, +, ^, and character classes, are nearly identical everywhere, so what you learn transfers directly. The small differences are in advanced features and syntax, such as how you write the pattern (JavaScript uses /pattern/flags) and lookahead or named groups. When in doubt, set your tester to the exact language flavour you are targeting.

How do I match a special character like a dot or a plus sign?

Put a backslash in front of it to escape it. \. matches a literal dot, \+ matches a plus sign, and \? matches a question mark. Without the backslash, those characters are treated as metacharacters with special meaning. Inside a character class, most of them lose their power automatically, so [.+] already means a literal dot or plus.

What is the difference between greedy and lazy quantifiers?

By default, quantifiers like * and + are greedy: they match as much text as possible and then back off if needed. Adding a ? makes them lazy, so they match as little as possible. For example, on the text "<a><b>", the greedy <.+> grabs the whole thing, while the lazy <.+?> stops at the first closing bracket. Reach for lazy when you extract short chunks from longer text.

Should I use regex to validate email addresses?

For a quick check that an input looks roughly like an email, a simple pattern is fine. But regex cannot truly confirm an address exists or is spelled the way the owner intended, and a fully correct email regex is enormous and unreadable. The reliable approach is a light regex sanity check followed by a confirmation email that the user must click. Do not try to catch every edge case in the pattern.

Where can I practise regex as a beginner?

Use an interactive tester like regex101.com or regexr.com. They highlight matches live, explain each part of your pattern, and list captured groups, which makes learning much faster than trial and error in code. Start with tiny goals such as matching a single digit or a whole word, then work up to phone numbers and dates. You can also apply regex right away in the Find box of VS Code.