Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Regular expression

Регулярное выражение

A text pattern that describes what to look for. You can use it to check whether text has the expected format, pull out specific parts, or replace matching fragments.

Also known as: regex, регулярка

What a regular expression does

A regular expression, or regex, finds text that follows a rule even when you don't know the exact text in advance. Searching for AB-1234 finds that one code. Searching with [A-Z]{2}-[0-9]{4} can find AB-1234, XY-0087, or any other fragment made up of two uppercase ASCII letters, a hyphen, and four ASCII digits.

How to read a pattern

Most letters and digits in a regex stand for themselves. Other characters tell the regex engine which characters can match, how often they can appear, or where the match can occur.

  • A character class such as [A-Z] matches one character from a set. [abc] means one of those three letters, not the string abc.
  • A quantifier applies to the preceding item. [0-9]{4} requires four digits, x? allows zero or one x, and x+ requires at least one x. x* allows zero or more.
  • A group such as (ab) lets you treat multiple pattern elements as a unit and retrieve the text they matched. For example, (ab)+ matches one or more copies of ab.
  • An alternative uses |: cat|dog matches either word. A dot is also special: . usually matches any character except a line break, whereas \. matches a literal dot.

A search can return a match from the middle of a string. Format validation usually requires a match across the entire input. Some libraries offer a full-match method; otherwise, check how your engine's start and end anchors behave, particularly around line breaks.

Example: extract a code or validate a field

import re

code = re.compile(r"([A-Z]{2})-([0-9]{4})")
match = code.search("Processed order AB-1234")
if match:
    print(match.group(0))  # AB-1234
    print(match.groups())  # ('AB', '1234')

assert code.fullmatch("AB-1234") is not None
assert code.fullmatch("Order AB-1234") is None

The search extracts a code from a message, and the parentheses make its prefix and numeric part available separately. The validation step rejects a value with extra text before the code. The r prefix is Python syntax for a raw string; it's useful when a pattern contains backslashes that should reach the regex engine unchanged.

When to use regex

Regex is useful for pulling fields out of plain-text logs, checking an identifier's shape, and making patterned replacements in an editor. Use a plain string method for a fixed prefix or exact substring, and a parser for a structured format such as JSON. A regex can recognize the shape of a date, but a date parser can tell you whether the day actually exists.

Don't assume a pattern behaves identically in every language. For example, Python's \d matches Unicode decimal digits by default, while [0-9] explicitly limits the match to ASCII digits.

What interviewers look for

You may be asked to write a pattern, trace its matches, or explain what each part captures. Clarify whether you need to find a fragment or validate a whole field, then test a valid input, an input with extra text, and a boundary case such as an empty string or a trailing newline.