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 x+iy
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:
Click for copyable input
The rule picks out each piece of the complex numbers:
Click for copyable input
The fact that these expressions have different full forms means that you cannot use x_+Iy_ to match a complex number:
Click for copyable input
The pattern here matches both ordinary integers, and complex numbers where both the real and imaginary parts are integers:
Click for copyable input

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:
Click for copyable input
This is the full internal form of the expression:
Click for copyable input
This is what you get by applying a transformation rule to all powers in the expression:
Click for copyable input
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 a+bx
x_^n_xn with n0,1
x_^n_.xn 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 x:
Click for copyable input
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:
Click for copyable input
The definition applies in the second and third cases:
Click for copyable input