Regular Expression
Subject: Computer and Technology noun /ˈrɛɡ.jə.lər ɪkˈsprɛʃ.ən/ 1 definition
- computer-science
- pattern-matching
- text-processing
- formal-languages
-
Definition (noun)
Entry #153 · UpdatedA Regular Expression (commonly abbreviated regex or regexp) is a formal string notation combining literal characters and special metacharacters—such as character classes, quantifiers, and anchors—to specify a text-matching pattern. Pattern engines compile or interpret these expressions to scan input strings for format validation, substring search, structured extraction via capture groups, and automated find-and-replace operations. Grounded in formal language theory to define regular languages recognizable by finite automata, modern regex dialects frequently add extensions such as backreferences and lookarounds, though they remain ill-suited for parsing arbitrarily nested structures like balanced markup or programming language syntax.
Common Forms
POSIX Basic Regular Expressions (BRE): The traditional Unix dialect used by tools such as
grepandsed, where grouping parentheses and interval braces require backslash escaping (\(...\),\{m,n\}) while characters such as+,?, and|behave as literals.POSIX Extended Regular Expressions (ERE): A standardized Unix dialect supported by
grep -Eandawkthat treats+,?,|, parentheses, and interval braces as active metacharacters without requiring backslash prefixes.Perl-Compatible Regular Expressions (PCRE): A feature-rich backtracking dialect—popularized by Perl and mirrored across languages such as Python, JavaScript, Java, and PHP—that adds shorthand character classes (
\d,\w), lazy quantifiers, lookaround assertions, and backreferences.Finite-Automaton (Linear-Time) Engines: Non-backtracking implementations used in engines such as RE2, Go's
regexp, and Rust'sregexcrate that omit backreferences and arbitrary lookarounds to guaranteeO(n)worst-case matching time and prevent catastrophic backtracking.
Example
A log-ingestion pipeline applies the pattern
^(\d{4}-\d{2}-\d{2}) ERROR \[([A-Z0-9-]+)\]to incoming records and extracts both the ISO date2026-11-17and the service identifierAUTH-402from the line2026-11-17 ERROR [AUTH-402] Token expired. By skipping informational entries and capturing only those two fields, the pipeline indexes structured failure events without requiring a custom string parser.