WolframLang: Pattern Syntax

By Xah Lee. Date: . Last updated: .

Here's a list of all pattern syntax. 〔see Pattern Matching

Literal Expression

Literal expression matches itself. For example: symbol `a`, number `3`, string `"abc"`, all match themselves, and arbitrary expression `f[a,b,c]` matches itself.

```ReplaceAll[ {3,4}, 4 -> x]
(* {3, x} *)

ReplaceAll[ {a, f[3], f[9]}, f[9] -> g[9]]
(* {a, f[3], g[9]} *)

ReplaceAll[ {2/3, {4, 8}, {c}}, 2/3 -> 1/3]
(* {1/3, {4, 8}, {c}} *)
```

Blank Pattern

`_`

match any Expression

same as `Blank[]`

```ReplaceAll[ {3,4}, _ -> x]
(* x *)

ReplaceAll[ {a, f[3], f[9]}, f[_] -> g ]
(* {a, g, g} *)

ReplaceAll[ {a, f[3], g[4], f[5]}, _[_] -> g]
(* {a, g, g, g} *)

ReplaceAll[ {{2}, {4, 8}, {c}}, {_ , _} -> xpair]
(* {{2}, xpair, {c}} *)
```

for `ReplaceAll` usage, see WolframLang: Change Expression by Pattern

`_h`

same as `Blank[h]`

```Cases[ {3, 4.5, 5/6, {x, y}}, _Integer, {1} ]
(* {3} *)

Cases[ {3, 4.5, 5/6, {x, y}}, _Rational, {1} ]
(* {5/6} *)

Cases[ {3, 4.5, 5/6, {x, y}}, _Real, {1} ]
(* {4.5} *)

Cases[ {3, 4.5, 5/6, {x, y}}, _List, {1} ]
(* {{x, y}} *)

Cases[ {3, f[a], f[b]} , _f , {1}]
(* {f[a], f[b]} *)

```

Basic Named Pattern

`x_`

any expression, giving it a name x

same as `Pattern[x, Blank[]]`

Pattern
```ReplaceAll[ {3,4}, x_ -> x+y ]
(* {3 + y, 4 + y} *)```
`x_h`

any expression with Head h, giving it a name x

same as `Pattern[x, Blank[h]]`

```ReplaceAll[ {3, {5,6}}, x_List -> f[x] ]
(* f[{3, {5, 6}}] *)

ReplaceAll[ {3, f[a]}, x_f -> y ]
(* {3, y} *) ```

Repetition, One or More, Zero or More

`__`

any sequence of one or more expressions. (2 LOW LINE characters)

```Cases[ {3, {}, {5, 6}, {7, f, 9}}, {__}, {1} ]
(* {{5, 6}, {7, f, 9}} *)
```
`___`

any sequence of zero or more expressions (3 LOW LINE characters)

```Cases[ {3, {}, {5, 6}, {7, f, 9}}, {___}, {1} ]
(* {{}, {5, 6}, {7, f, 9}} *)
```
`__h` and `___h`

sequences of expressions, each with Head h

same as `BlankSequence[h]` or `BlankNullSequence[h]`

`x__` and `x___`

sequences of expressions, giving it a name x

same as `Pattern[x, BlankSequence[]]` or `Pattern[x, BlankNullSequence[]]`

`x__h` and `x___h`

sequences of expressions with Head h, giving it a name x

same as `Pattern[x, BlankSequence[h]]` or `Pattern[x, BlankNullSequence[h]]`

Repeated Pattern

`pattern..`

a pattern repeated one or more times

`pattern...`

a pattern repeated zero or more times

`Repeated[pattern, spec]`

a pattern repeated according to spec

Conditional Patterns

`pattern?test`

a pattern that yields True when test is applied to its value

`pattern/;cond`

a pattern for which cond evaluates to True

alternatives

`p1|p2|etc`

a pattern that matches at least one of the pattern.

same as `Alternatives[p1, p2, etc]`

Literal Pattern

`HoldPattern[pattern]`

a pattern not evaluated

`Verbatim[expr]`

an expression to be matched verbatim

Naming a Generic Pattern

`x:pattern`

a pattern, giving it a name x

same as `Pattern[x, Blank[]]`

Pattern Negation

`Except[c]`

any expression except one that matches c

`Except[c, pattern]`

any expression matching pattern, except one that matches c

`PatternSequence[p1, p2, etc]`

a sequence of patterns

`OrderlessPatternSequence[p1, p2, etc]`

an orderless sequence of patterns

`KeyValuePattern[{‹key1› → ‹val1›, etc}]`

an orderless sequence of key → val pairs

`x_:v`

an expression with default value v

`x_h:v`

an expression with Head h and default value v

`x_.`

an expression with a globally defined default value

`Optional[x_h]`

an expression that must have Head h, and has a globally defined default value

`OptionsPattern[]`

a sequence of options

`Longest[pattern]`
`Shortest[pattern]`