Skip to main content

Questions on expression testing and manipulation through pattern matching and constructing efficient patterns.

The pattern matching is at the core of Mathematica and it is tightly integrated with and applicable to nearly every part of the system. All top-level computations are done in essence by expression manipulation through pattern matching. This symbolic pattern language generalizes concepts like regular expressions to describe general patterns for arbitrary symbolic structures using pattern-objects, like the Blank family (_, __, ___), Repeated (.., ...), Alternatives (|), Except, Optional (:), etc.

Accordingly, patterns and pattern matching can be used in:

  • replacing parts of expressions using replacement rules (Rule -> and RuleDelayed :> used with the Replace family of functions); consider adding the tag ;
  • expression testing (Condition /;, PatternTest ? with MatchQ, MemberQ, FreeQ, etc.); consider adding the tag ;
  • selecting subexpressions matching a pattern (Cases, DeleteCases, Position, Count, etc.); consider adding the tag ;
  • branching control structures forking if a pattern is matched (If, Switch, Which, etc.);
  • function definition and assignments (in costructs like f[x_Real:0., y_List]:=...); consider adding the tag .

Do not use this tag for pattern matching in strings. For such questions please use the tag or .

Useful links

Example questions