Fundamentals

What Is Regex? Definition, Examples & How It Works

Regex—short for regular expression—is a compact way to describe text patterns so software can search, validate, extract, split, or replace matching text.

Regex definition: a regular expression is a pattern that describes a set of strings. Instead of searching for one exact word, you describe the structure you want to find.

What does Regex mean?

Regex is a common abbreviation for regular expression. Developers use regular expressions when text follows a recognizable structure: numbers, IDs, log lines, file extensions, URLs, dates, tags, or pieces of larger strings.

For example, \d+ describes one or more digits. It can find 42 inside “Order 42” without knowing the number in advance.

What is Regex used for?

  • Search: find text that follows a pattern.
  • Validation: check whether a complete value has an expected format.
  • Extraction: capture useful fields such as IDs or numbers.
  • Replacement: normalize or transform matching text.
  • Splitting: divide text using a pattern as the separator.

Regex is common in programming languages, text editors, IDEs, command-line tools, data cleaning workflows, and log analysis.

A simple Regex example

Suppose an internal code must contain three uppercase letters, a hyphen, and four digits:

^[A-Z]{3}-\d{4}$

This pattern matches DEV-2026 and OPS-0001, while rejecting DE-2026 or DEV2026.

How to read a Regex pattern

PartMeaning
^Start of the string
[A-Z]One uppercase letter
{3}Exactly three times
-A literal hyphen
\dA digit
{4}Exactly four digits
$End of the string

Read patterns in small pieces. Regex becomes much easier when you translate each token into a requirement instead of trying to memorize the full expression.

Finding text is different from validating a value

The expression \d{3} finds three consecutive digits anywhere. The expression ^\d{3}$ requires the entire value to contain exactly three digits. Anchors change the question you are asking.

Regex engines are similar, not identical

JavaScript, Python, Java, .NET, PCRE, grep, and other tools share many regex concepts, but syntax and feature support can differ. Always test a pattern in the same engine used by your application.

For JavaScript-specific behavior, see our JavaScript Regex guide.

When should you not use Regex?

Regex is not automatically the best tool for every structured value. A parser is often safer for complex HTML/XML, a date library is better for calendar logic, and numeric conversion is clearer for numeric ranges. Use regex when the problem is genuinely about text patterns.

How to learn Regex without memorizing everything

Start with literals, character classes, quantifiers, and anchors. Then add groups, alternation, and finally lookarounds. More importantly, always keep two lists: examples that should match and examples that must not match.

Learn with 24 levelsTry practice problems

Keep learning

← Back to learning hub