Multilingual Regular Expression Syntax (Pattern)

About

Regular expression are Expression that defines a pattern in text.

This is therefore a language that permits to define structure of a text.

They are a mathematically-defined concept, invented by Stephen Kleene in 1956. The original version defined expressions using:

The Glob language behave a lot like a regular expression but is not. The difference is mostly in the signification of the star and is use a lot in Shell in order to match file name. See Glob - Globbing (Wildcard expression).

Example

Simple Date Parsing

The below expression describes a date:

\d{4}-\d{2}-\d{2}

where:

It permits to parse ISO 86021 string date such as

2022-07-28

Log Parsing

Java regular expression used to parse log message at Twitter circa 2010

^(\\w+\\s+\\d+\\s+\\d+:\\d+:\\d+)\\s+
([^@]+?)@(\\s+)\\s+(\\S+):\\s+(\\S+)\\s+(\\S+)
\\s+((?:\\S+?,\\s+)*(?:\\S+?))\\s+(\\S+)\\s+(\\S+)
\\s+\\[([^\\]]+)\\]\\s+\"(\\w+)\\s+([^\"\\\\]*
(?:\\\\.[^\"\\\\]*)*)\\s+(\\S+)\"\\s+(\\S+)\\s+
(\\S+)\\s+\"([^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)
\"\\s+\"([^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\"\\s*
(\\d*-[\\d-]*)?\\s*(\\d+)?\\s*(\\d*\\.[\\d\\.]*)?
(\\s+[-\\w]+)?.*$

where:

Usage

Regular expressions are used in many systems.

  • File Search. E.g., UNIX a.*b. known as glob
  • Data Structure Definition. Example:
    • DTD’s describe XML tags with a RE format like person (name, addr, child*).
  • Text extraction
  • Text matching

Syntax

/regularExpression/modifier

where:

See meta to get an overview of the most important regexp (symbols|token) that have a meaning in the context of regular expression.

Regular language and Automata

Regular expressions are implemented with context-free grammars and are the building block of regular language.

Stephen Kleene was the fellow who invented regular expressions and showed that they describe the same languages that finite automata describe. Most regexp-packages transform a regular expression into an automaton based on nondeterministic automata (NFA). The automaton does then the parse work.

Regular expression can be matched against text using a Definite Finite Automaton (DFA). DFA's have the important property of running on arbitrary text of length n in O(n) time and using O(1) space. Presented with a regular expression and a candidate text, a DFA decides whether the text matches the expression.

Engine / Grammar

The syntax can differ from one implementation to an other but then tend to follow the same principal.

The most known implementation is PCRE that is followed by most of the language

Visualisation

Regular expression are visualized via rail road diagram.

Cheatsheet

Play: Crossword


Powered by ComboStrap