A Beginner's Guide to Regular Expressions (Regex)

Published June 30, 2026

Prepared by Innealan Editorial

Interested in advertising in this spot? Contact us for sponsorship options

Regular expressions describe patterns in text. They are useful for validation, search-and-replace, and extraction when the pattern is genuinely regular.

The essential building blocks

PatternMeaning
.Any character except newline
\dAny digit (0-9)
\wAny “word” character (letters, digits, underscore)
\sAny whitespace character
*Zero or more of the previous token
+One or more of the previous token
?Zero or one of the previous token (also makes quantifiers “lazy” when doubled, e.g. *?)
{n,m}Between n and m repetitions
^ / $Start / end of string (or line, with the m flag)
[abc]Any one of the characters a, b, or c
(...)A capturing group
(?:...)A non-capturing group
|Alternation (“or”)

Worked examples

Match a simple email address:

^[\w.+-]+@[\w-]+\.[a-zA-Z]{2,}$

Extract all hashtags from a string:

#\w+

Match a US-style phone number in a few common formats:

\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}

Flags that change behavior

A word of caution

Regex is suitable for finding a line such as ERROR 503 in a log. It is a poor choice for parsing nested or context-sensitive formats such as HTML or JSON, where a real parser understands the structure. Keep a test string beside a pattern so a later edit does not quietly broaden what it matches.

Test your own patterns against sample text, with matches highlighted live, using our Regex Tester.

Sources and further reading