Regular Expression
- In Turkish
- Düzenli ifade
In short
A regular expression, or regex, is a pattern written in a compact syntax that describes text to search for, validate, extract or replace within strings.
What is a regular expression?
A regular expression is a small pattern language for describing text. The pattern \d{3}-\d{4} means "three digits, a hyphen, then four digits", and a regex engine can use it to check whether a string matches, find every match in a document, pull out parts of each match or replace them. Almost every programming language, text editor and command-line tool such as grep and sed supports regular expressions.
Patterns are built from a few pieces. Literal characters match themselves, character classes like [a-z] or \d match one character from a set, quantifiers like *, +, ? and {2,5} say how many times something repeats, and the anchors ^ and $ pin a match to the start or end of the text. Parentheses create groups that can be captured and reused, and flags change behavior, such as i for case-insensitive matching or g for finding all matches in JavaScript.
A regex is like a search filter with wildcards: instead of looking for one exact word, you describe the shape of what you want. Common uses include validating input such as postal codes, parsing log lines, finding and renaming things across a codebase and cleaning up data. Regexes are also easy to get wrong and hard to read, so it's best to keep them short, comment them and test them against both matching and non-matching examples.
Regular expressions are often confused with the glob patterns used for file names. In a glob, *.txt means "any file ending in .txt", while in a regex * means "repeat the previous item", so the equivalent regex is ^.*\.txt$. Regexes are also a poor tool for parsing nested formats like HTML or JSON, which need a real parser. Patterns with nested repetition can take exponential time on certain inputs, a denial-of-service risk known as ReDoS.
Key takeaways
- A regex is a pattern that describes the shape of text to match.
- Character classes, quantifiers, anchors and groups are its main building blocks.
- It is used for searching, validating, extracting and replacing text.
- Glob patterns for file names look similar but follow different rules.
- Complex regexes are hard to read and can be slow, so keep them small and tested.
Example
const log = "2026-09-30 ERROR [auth] Login failed for user=ada";
// Capture the date, the level and the user name
const pattern = /^(\d{4}-\d{2}-\d{2}) (\w+) .*user=(\w+)$/;
const match = log.match(pattern);
if (match) {
const [, date, level, user] = match;
console.log(date, level, user); // 2026-09-30 ERROR ada
}
// Replace every digit with #
console.log("Call 555-0142".replace(/\d/g, "#")); // Call ###-####Readers ask
What does regex stand for?
Regex is short for regular expression, a term from formal language theory in the 1950s. The spelling regexp is also common, for example in JavaScript's RegExp object.
Are regular expressions the same in every language?
The basics are shared, but details differ between flavors such as POSIX, PCRE, JavaScript, Python and .NET. Features like lookbehind, named groups and Unicode classes vary, so test a pattern in the language where it will actually run.
Should I validate email addresses with a regex?
A simple regex can catch obvious typos, but a fully correct email pattern is extremely complex. The reliable check is to send a confirmation message to the address.
See also
- StringProgramming Fundamentals, p. 54A string is a data type that represents text as an ordered sequence of characters, such as a name, a sentence, a URL, or the contents of a file.
- Input ValidationSecurity, p. 18Input validation is the practice of checking that data entering a program has the expected type, format and range before it is used, and rejecting the rest.
- AlgorithmProgramming Fundamentals, p. 2An algorithm is a finite, step-by-step set of instructions for solving a problem or completing a task, such as sorting a list or finding the shortest route.
- PerlProgramming Languages, p. 22Perl is a mature, dynamic scripting language famous for text processing and built-in regular expressions, long used for system administration and web scripts.
- Full-Text SearchDatabases, p. 22Full-text search is a technique that finds documents containing given words or phrases by looking them up in a text index, then ranks the results by relevance.
Spotted a mistake or something missing on this page?Suggest an edit