Patterns

One of the unique strengths of Mathematica's core 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, Mathematica's pattern language generalizes concepts like regular expressions to describe general patterns for arbitrary symbolic structures.

ReferenceReference

Basic Pattern Objects

_ (Blank)— any expression (a "blank" to be filled in)

x_ any expression, to be referred to as x

__ (BlankSequence)— any sequence of one or more expressions

___ (BlankNullSequence)— any sequence of zero or more expressions

Composite Patterns

p|p|p (Alternatives) any of several alternatives

p.. (Repeated), p... (RepeatedNull) a pattern to be repeated

x:p (Pattern) — an arbitrary pattern, to be referred to as x

Except anything except a specified pattern

Longest, Shortest longest, shortest possible matches

OptionsPattern ▪ PatternSequence ▪ Verbatim ▪ HoldPattern

Restrictions on Patterns

_h pattern with a specified head h (e.g. _Integer)

Condition (/;) condition on a pattern (e.g. )

PatternTest (?) pattern with a function test (e.g. _?NumberQ)

Pattern Defaults

_:e (Optional) — pattern that defaults to e if omitted

_. (Optional) — pattern with predefined default

Default predefined default arguments for a function

Attributes Affecting Patterns

Orderless ▪ Flat ▪ OneIdentity

String Patterns »

StringExpression symbolic string pattern

Pattern Matching Functions »

MatchQ ▪ Cases ▪ Position ▪ Count ▪ ...

New to Mathematica? Find your learning path »
Have a question? Ask support »