Patterns for Some Common Types of Expression

Using the objects described in "Introduction to Patterns", you can set up patterns for many kinds of expressions. In all cases, you must remember that the patterns must represent the structure of the expressions in the Wolfram Language internal form, as shown by FullForm.

Especially for some common kinds of expressions, the standard output format used by the Wolfram Language is not particularly close to the full internal form. But it is the internal form that you must use in setting up patterns.

n_Integeran integer n
x_Realan approximate real number x
z_Complexa complex number z
Complex[x_,y_]a complex number
Complex[x_Integer,y_Integer]a complex number where both real and imaginary parts are integers
(r_Rational|r_Integer)rational number or integer r
Rational[n_,d_]a rational number
(x_/;NumberQ[x]&&Im[x]==0)a real number of any kind
(x_/;NumberQ[x])a number of any kind

Some typical patterns for numbers.

Here are the full forms of some numbers.
In[1]:=
Click for copyable input
Out[1]//FullForm=
The rule picks out each piece of the complex numbers.
In[2]:=
Click for copyable input
Out[2]=
The fact that these expressions have different full forms means that you cannot use x_+Iy_ to match a complex number.
In[3]:=
Click for copyable input
Out[3]//FullForm=
The pattern here matches both ordinary integers, and complex numbers where both the real and imaginary parts are integers.
In[4]:=
Click for copyable input
Out[4]=

As discussed in "Symbolic Computation", the Wolfram Language puts all algebraic expressions into a standard form, in which they are written essentially as a sum of products of powers. In addition, ratios are converted into products of powers, with denominator terms having negative exponents, and differences are converted into sums with negated terms. To construct patterns for algebraic expressions, you must use this standard form. This form often differs from the way the Wolfram Language prints out the algebraic expressions. But in all cases, you can find the full internal form using FullForm[expr].

Here is a typical algebraic expression.
In[5]:=
Click for copyable input
Out[5]=
This is the full internal form of the expression.
In[6]:=
Click for copyable input
Out[6]//FullForm=
This is what you get by applying a transformation rule to all powers in the expression.
In[7]:=
Click for copyable input
Out[7]=
x_+y_a sum of two or more terms
x_+y_.a single term or a sum of terms
n_Integerx_an expression with an explicit integer multiplier
a_.+b_.x_a linear expression
x_^n_ with
x_^n_. with n0
a_.+b_.x_+c_.x_^2a quadratic expression with nonzero linear term

Some typical patterns for algebraic expressions.

This pattern picks out linear functions of .
In[8]:=
Click for copyable input
Out[8]=
x_List or x:{___}a list
x_List/;VectorQ[x]a vector containing no sublists
x_List/;VectorQ[x,NumberQ]a vector of numbers
x:{___List} or x:{{___}...}a list of lists
x_List/;MatrixQ[x]a matrix containing no sublists
x_List/;MatrixQ[x,NumberQ]a matrix of numbers
x:{{_,_}...}a list of pairs

Some typical patterns for lists.

This defines a function whose argument must be a list containing lists with either one or two elements.
In[9]:=
Click for copyable input
The definition applies in the second and third cases.
In[10]:=
Click for copyable input
Out[10]=