Regular Expression
Also known as: Regex, Regexp, Rational expression
Compact pattern language for describing sets of strings, used to search, match, validate and transform text in programs and command-line tools.
A regular expression is a sequence of characters that defines a search pattern. Ordinary characters match themselves, while special characters, called metacharacters, express more general rules: a period matches any character; an asterisk means zero or more repetitions of what precedes it; square brackets define a set of allowed characters; and a caret or dollar sign anchors a match to the start or end of a line. The pattern [0-9]+ matches any run of one or more digits. Regular expressions are built into most programming languages, text editors and command-line tools such as grep and sed.
The concept comes from theoretical computer science. The mathematician Stephen Kleene described regular sets in the 1950s to characterize what simple automata can recognize. Ken Thompson brought them into practical computing, implementing regular expression search in the QED text editor and describing his method in a 1968 paper, and later in the Unix editor ed, from which the grep command was derived. Perl, released in 1987, extended the syntax considerably, and its flavor became the model for regex libraries in many other languages, including Python and JavaScript.
Regular expressions are a powerful tool for log analysis, data cleaning, input validation and search-and-replace across files, often doing in one line what would otherwise take many. Their compact syntax is also notoriously hard to read, and an old programmers' joke holds that solving a problem with a regex leaves two problems. Some patterns trigger catastrophic backtracking in common engines, taking exponential time on certain inputs, which attackers can exploit as a denial of service known as ReDoS. Engines based on Thompson's automaton approach, such as RE2, avoid this by guaranteeing linear-time matching.
Key points
- Regular expressions describe text patterns using ordinary characters plus metacharacters for repetition, choice and position.
- Stephen Kleene formalized regular sets, and Ken Thompson brought regular expressions into text editors and Unix tools.
- Perl's extended syntax became the template for regex libraries in many later languages.
- Poorly constructed patterns can take exponential time, enabling ReDoS attacks against vulnerable engines.
Related entries
Ken Thompson
American computer scientist who created the Unix operating system and the B language and later co-designed the UTF-8 encoding and the Go language.
Shell Scripting
Writing programs for a command-line shell, such as Bash, that automate sequences of commands, file operations and system administration tasks.
Command-Line Interface
Text-based interface in which users interact with a computer by typing commands and reading text output rather than using graphical menus.
Python
High-level, general-purpose programming language created by Guido van Rossum, known for readable syntax and widely used in data science and AI.
JavaScript
High-level programming language of the web, created at Netscape in 1995, that runs in every major browser and on servers through runtimes like Node.js.
Linting
Automated checking of source code for likely errors, suspicious constructs and style violations using a tool called a linter.
Denial-of-Service Attack
Attack that makes a service, website or network unavailable to legitimate users, typically by overwhelming it with traffic or requests.
Bash
Unix shell and command language written for the GNU Project, long the default interactive shell and scripting language on Linux systems.