What is Regex?

Article

Regex (short for regular expression) is a sequence of characters that defines a search pattern. Regex is used to match, find, replace or validate strings — from simple word searches to complex text processing tasks. Every major programming language includes a built-in regex engine, making it one of the most universally applicable tools in a developer's toolkit.

Want to test a regular expression interactively?

Open Regex Tester →

History

Regular expressions originate from formal language theory. Mathematician Stephen Cole Kleene introduced them in the 1950s as a notation for describing regular languages. Ken Thompson implemented regex in the UNIX text editor ed and later in grep (1973), making them widely available to developers. Perl (1987) expanded the syntax significantly and popularized the PCRE (Perl-Compatible Regular Expressions) dialect that most modern languages follow today.

Basic Syntax

A regular expression is a pattern written between delimiters (in JavaScript: /pattern/flags). The pattern can contain literal characters, special metacharacters and quantifiers. The regex engine scans a string and reports every position where the pattern matches.

/* Email validation regex */
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

/* Match: alice@example.com */
/* No match: notanemail */

Core Concepts

Common Patterns

Flags

Flags modify how the regex engine interprets the pattern:

Use Cases

Rules to Remember

Frequently Asked Questions

What is regex?

Regex (short for regular expression) is a sequence of characters that defines a search pattern. It is used to match, find, replace or validate strings in text. Every major programming language supports regular expressions.

What does regex stand for?

Regex stands for regular expression. The term comes from formal language theory developed by mathematician Stephen Cole Kleene in the 1950s.

What is regex used for?

Regex is used for input validation (email, phone, password), search and replace in text editors, log file parsing, URL routing in web frameworks, and data extraction from unstructured text.

Is regex the same in all programming languages?

Most languages support a common core syntax based on PCRE (Perl-Compatible Regular Expressions), but there are differences in flags, some special characters and advanced features like lookbehind. JavaScript, Python, Java and PHP all use PCRE-compatible engines.

What is the difference between * and + in regex?

The * quantifier matches zero or more occurrences of the preceding element. The + quantifier matches one or more occurrences. So a* matches "", "a", "aa", ... while a+ matches "a", "aa", ... but not an empty string.

What does ^ mean in regex?

The ^ character has two meanings. At the start of a pattern it is an anchor that matches the beginning of the string (or line in multiline mode). Inside a character class [^abc] it means negation — match any character except those listed.

Related Articles

Sources