language-icon Old Web
English
Sign In

Pattern matching

In computer science, pattern matching is the act of checking a given sequence of tokens for the presence of the constituents of some pattern. In contrast to pattern recognition, the match usually has to be exact: 'either it will or will not be a match.' The patterns generally have the form of either sequences or tree structures. Uses of pattern matching include outputting the locations (if any) of a pattern within a token sequence, to output some component of the matched pattern, and to substitute the matching pattern with some other token sequence (i.e., search and replace). In computer science, pattern matching is the act of checking a given sequence of tokens for the presence of the constituents of some pattern. In contrast to pattern recognition, the match usually has to be exact: 'either it will or will not be a match.' The patterns generally have the form of either sequences or tree structures. Uses of pattern matching include outputting the locations (if any) of a pattern within a token sequence, to output some component of the matched pattern, and to substitute the matching pattern with some other token sequence (i.e., search and replace). Sequence patterns (e.g., a text string) are often described using regular expressions and matched using techniques such as backtracking. Tree patterns are used in some programming languages as a general tool to process data based on its structure, e.g., C#, Rust, Swift, Haskell, ML, Scala and the symbolic mathematics language Mathematica have special syntax for expressing tree patterns and a language construct for conditional execution and value retrieval based on it. For simplicity and efficiency reasons, these tree patterns lack some features that are available in regular expressions. Often it is possible to give alternative patterns that are tried one by one, which yields a powerful conditional programming construct. Pattern matching sometimes includes support for guards. Parsing algorithms often rely on pattern matching to transform strings into syntax trees. The first computer programs to use pattern matching were text editors. At Bell Labs, Ken Thompson extended the seeking and replacing features of the QED editor to accept regular expressions. Early programming languages with pattern matching constructs include SNOBOL from 1962, Soviet language Refal from 1968 with tree-based pattern matching, SASL from 1976, NPL from 1977, and KRC from 1981. Another programming language with tree-based pattern matching features was Fred McBride's extension of LISP, in 1970.

[ "Algorithm", "Computer vision", "Artificial intelligence", "Pattern recognition", "Programming language", "Factor oracle", "Suffix automaton", "Byte pair encoding", "Aho–Corasick string matching algorithm", "Pattern calculus" ]
Parent Topic
Child Topic
    No Parent Topic