Patterns

One of the unique strengths of the Wolfram Language is its powerful and succinct—yet highly readable—symbolic pattern language. Convenient both for immediate use in individual functions, and for systematic large-scale programming, the Wolfram Language's pattern language generalizes concepts like regular expressions to describe general patterns for arbitrary symbolic structures.

Basic Pattern Objects

(RefLink[_, paclet:ref/Blank])  ((RefLink[Blank, paclet:ref/Blank]))— any expression (a "blank" to be filled in)

(RefLink[x_, paclet:ref/Blank]) — any expression, to be referred to as x

(RefLink[__, paclet:ref/BlankSequence]) ((RefLink[BlankSequence, paclet:ref/BlankSequence]))— any sequence of one or more expressions

(RefLink[___, paclet:ref/BlankNullSequence]) ((RefLink[BlankNullSequence, paclet:ref/BlankNullSequence]))— any sequence of zero or more expressions

Composite Patterns

(RefLink[p|p|p, paclet:ref/Alternatives]) ((RefLink[Alternatives, paclet:ref/Alternatives])) — any of several alternatives

(RefLink[p.., paclet:ref/Repeated]) ((RefLink[Repeated, paclet:ref/Repeated])), (RefLink[p..., paclet:ref/RepeatedNull]) ((RefLink[RepeatedNull, paclet:ref/RepeatedNull])) — a pattern to be repeated

(RefLink[x:p, paclet:ref/Pattern]) ((RefLink[Pattern, paclet:ref/Pattern])) — an arbitrary pattern, to be referred to as x

(RefLink[Except, paclet:ref/Except]) — anything except a specified pattern

(RefLink[Longest, paclet:ref/Longest]), (RefLink[Shortest, paclet:ref/Shortest]) — longest, shortest possible matches

(RefLink[OrderlessPatternSequence, paclet:ref/OrderlessPatternSequence]) — elements in any order

(RefLink[KeyValuePattern, paclet:ref/KeyValuePattern]) — an association or list of rules containing specified elements

Restrictions on Patterns

(RefLink[_h, paclet:ref/Blank]) — pattern with a specified head h (e.g. _(RefLink[Integer, paclet:ref/Integer]))

(RefLink[Condition, paclet:ref/Condition]) ((RefLink[/;, paclet:ref/Condition])) — condition on a pattern (e.g. x_/;x>7)

(RefLink[PatternTest, paclet:ref/PatternTest]) ((RefLink[?, paclet:ref/PatternTest])) — pattern with a function test (e.g. _?(RefLink[NumberQ, paclet:ref/NumberQ]))

Pattern Defaults

(RefLink[_:e, paclet:ref/Optional]) ((RefLink[Optional, paclet:ref/Optional])) — pattern that defaults to e if omitted

(RefLink[_., paclet:ref/Optional]) ((RefLink[Optional, paclet:ref/Optional])) — pattern with predefined default

(RefLink[Default, paclet:ref/Default]) — predefined default arguments for a function

Attributes Affecting Patterns

(OrangeLink[String Patterns », paclet:guide/StringPatterns])

(RefLink[StringExpression, paclet:ref/StringExpression]) — symbolic string pattern

(OrangeLink[Pattern Matching Functions », paclet:guide/PatternMatchingFunctions])