Patterns are a fundamental concept in the Wolfram Language. The pattern _ (read “blank”) stands for anything.

MatchQ tests whether something matches a pattern.

MatchQ[{a,x,b},{_,x,_}]

MatchQ[{a,b,c},{_,x,_}]

MatchQ[{a,a},{_,_}]

MatchQ[{a,a,a},{_,_}]

MatchQ lets you test one thing at a time against a pattern. Cases lets you pick out all the elements (“cases”) in a list that match a particular pattern.

Cases[{{a,a},{b,a},{a,b,c},{b,b},{c,a},{b,b,b}},{_,_}]

Cases[{{a,a},{b,a},{a,b,c},{b,b},{c,a},{b,b,b}},{b,_}]

MatchQ[#,{b,_}]&/@{{a,a},{b,a},{a,b,c},{b,b},{c,a},{b,b,b}}

Select[{{a,a},{b,a},{a,b,c},{b,b},{c,a},{b,b,b}},MatchQ[#,{b,_}]&]

In a pattern, a|b indicates “either a or b”.

Cases[{{a,a},{b,a},{a,b,c},{b,b},{c,a},{b,b,b}},{a|b,_}]

Let’s look at another example, based on creating a list, then picking out elements that match particular patterns.

IntegerDigits[Range[100,500,55]]

Cases[IntegerDigits[Range[100,500,55]],{_,_,5}]

Cases[IntegerDigits[Range[100,500,55]],{_,1|2,_}]

The notation __ (“double blank”) in a pattern indicates any sequence of things.

Cases[{{a,a},{b,a},{a,b,c},{b,b},{c,a},{b,b,b}},{__,b}]

Cases[{{a,a},{b,a},{a,b,c},{b,b},{c,a},{b,b,b}},{__,a|b}|{c,__}]

Patterns aren’t just about lists; they can involve anything.

Cases[{f[1],g[2,3],{a,b,c},f[x],f[x,x]},f[_]]

One of the many uses of patterns is to define replacements. /. (“slash dot”) performs a replacement.

{a,b,a,a,b,b,a,b} /. b->Red

{{1,a},{1,b},{1,a,b},{2,b,c},{2,b}}/.{1,_}->Red

{{1,a},{1,b},{1,a,b},{2,b,c},{2,b}}/. {{1,_}->Red,{__,b}->Yellow}

The “blank” pattern _ matches absolutely anything. This means, for example, that {_,_} matches any list of two elements. But what if you want to insist that the two elements be the same? You can do that using a pattern like {x_,x_}.

Cases[{{a,a,a},{a,a},{a,b},{a,c},{b,a},{b,b},{c},{a},{b}}, {_,_}]

Cases[{{a,a,a},{a,a},{a,b},{a,c},{b,a},{b,b},{c},{a},{b}}, {x_,x_}]

x_ is an example of a named pattern. Named patterns are especially important in replacements, because they give one way to make use of parts of what one’s replacing.

{{1,},{1,},{1,,},{2,,},{2,}} /. {1,x_}->{x,x,Yellow,x,x}

The form a->b is usually called a rule. If x_ appears on the left-hand side of a rule, then whatever the x_ matches can be referred to on the right-hand side as x.

{f[1],g[2],f[2],f[6],g[3]}/.f[x_]->x+10

You can use rules inside Cases as well.

Cases[{f[1],g[2],f[2],f[6],g[3]},f[x_]->x+10]

Later on, we’ll see how named patterns are crucial to defining your own functions in the Wolfram Language.

Find lists of length 3 or more beginning with 1 and ending with 9 in IntegerDigits[Range[1000]]. »

Find lists of three identical elements in IntegerDigits[Range[1000]]. »

In the digit lists for the first 1000 squares, find those that begin with 9 and end with 0 or 1. »

In IntegerDigits[Range[100]] replace all 0’s by Gray and all 9’s by Orange. »

Make a list of the digits of 2^1000, replacing all zeros by Red. »

Remove the vowels a, e, i, o and u from the list of characters in “The Wolfram Language”. »

Find a simpler form for Select[IntegerDigits[2^1000],#==0||#==1&]. »

Find a simpler form for Select[IntegerDigits[Range[100,999]],First[#]==Last[#]&]. »

Do the names of pattern variables (x_, etc.) matter?

No. They just have to be consistent inside a given pattern. Different patterns can reuse the same name for different purposes, and the name can also appear outside the pattern.

What else can be used to define patterns?

We’ll discuss several more things in Section 41.

What’s the difference between | and ||?

p|q is a pattern construct, that matches either p or q. p||q is a logic construct, that tests whether p or q is True.

How does one say a|b?

Either “a or b” or “a vertical bar b”.

How is /. interpreted?

It’s the function ReplaceAll. Replace tries to replace a whole expression. ReplaceList gives a list of results from all possible ways to match a particular pattern.

If /. has several replacements, which one will it use?

It uses the first one that applies. If replacements apply to multiple levels of an expression, /. will use it on the outermost level.

Tech Notes