Nepedia
Back to Discover
Advanced Reader
💻

Regular expression

technology Maturity 7-9 Next article →

A regular expression, often shortened to regex or regexp, is a sequence of characters that defines a specific match pattern within text. These patterns allow computers to perform complex string-searching tasks. They are commonly used for "find" or "find and replace" operations. They also serve as tools for input validation, which ensures that typed data follows a specific format. In the field of theoretical computer science, regular expressions are a fundamental part of formal language theory. They provide a way to describe and classify different types of languages through mathematical notation.

To understand how a regex works, you must distinguish between two types of characters: literal characters and metacharacters. A literal character has a fixed meaning. For example, the letter "b" in a pattern will only match the letter "b" in the text. Metacharacters, however, act as instructions that represent broader rules. A dot (.), for instance, is a metacharacter that matches any single character except for a newline. By combining these, you can create very precise or very general patterns. A pattern like "seriali[sz]e" can match both "serialise" and "serialize" by using brackets to offer a choice.

Thompson-kleene-star
Thompson-kleene-star

Regular expressions use several specific operations to build complex rules. One common method is Boolean "or," which uses a vertical bar to separate different choices, such as "gray|grey." Another method is grouping, which uses parentheses to define the scope of these operators. Quantification is also essential. This tells the computer how many times a character or group should repeat. The question mark (?) indicates zero or one occurrence. The asterisk (*) represents zero or more occurrences, a concept known as the Kleene star. The plus sign (+) indicates one or more occurrences. You can even specify exact numbers, such as requiring a character to appear exactly five times.

Stephen Cole Kleene
Stephen Cole Kleene

The history of these patterns began in 1951 with the American mathematician Stephen Cole Kleene. He formalized the concept of regular languages while working in the subfields of automata theory and formal language theory. His work was originally motivated by attempts to describe early artificial neural networks. By 1968, regular expressions entered popular use in two main areas: pattern matching in text editors and lexical analysis in compilers. Ken Thompson was a key figure in this era. He integrated Kleene's notation into the QED editor. Thompson even used just-in-time compilation on an IBM 7094 system to make the matching process faster.

Thompson's work eventually led to the creation of the Unix editor "ed" and the famous search tool "grep." The name "grep" actually comes from the command "g/re/p," which stands for "Global search for Regular Expression and Print." During the 1970s, various versions of these expressions were used in Bell Labs programs like sed and AWK. In the 1980s, more complex versions emerged through the Perl language. This was built upon work by Henry Spencer, who wrote an implementation for Tcl called Advanced Regular Expressions. These different paths led to various standards, such as the POSIX.2 standard established in 1992.

Stephen Cole Kleene
Stephen Cole Kleene

Today, the significance of regular expressions is seen in almost every corner of computing. They are used in search engines and in the "find and replace" dialogs of word processors. Many programming languages, including Java, Python, and ECMAScript, include regex support in their standard libraries. Even large-scale web infrastructure relies on them; for example, Philip Hazel developed PCRE (Perl Compatible Regular Expressions) in 1997, which is used by the Apache HTTP Server. In recent years, the demand for speed has led companies to develop specialized hardware, such as FPGAs and GPUs, to run these engines even faster than a standard CPU.

Regular expressions connect to many broader technical systems. They are used in lexical analysis, which is the process of helping a compiler understand a programming language. They also play a role in document and database modeling. Since the 1960s, industry standards like ISO SGML have used the kernel of regular expressions to specify structures. Even modern languages like Raku use "Raku rules" to allow for more advanced parsing. From simple filename "globbing" to the complex logic of modern software, regular expressions remain a vital tool for organizing and processing the world's digital information.

699 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

🖼️ Images & Media (2)
File:Stephen Cole Kleene.jpg
Stephen Cole Kleene.jpg
File:Thompson-kleene-star.svg
Thompson-kleene-star.svg
More to explore

🔬 Go deeper

More advanced topics to explore

🪜 Step back

Simpler topics to build understanding

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.