Skip to content

MatchQ ​

Whether expr matches pattern, at the top level.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageMatchQ!
MatchQ(expr, pattern)True if expr matches pattern, False otherwise
Typed signatures
TypePackageApplies
(any, any) -> booleancombinatoricsalways

Domain: Collections

Details
  • Built directly on compute-engine's own wildcard grammar (BoxedExpression.match): _/_name matches one element, __/__name one-or-more, ___/___name zero-or-more — Wolfram's Blank/BlankSequence/BlankNullSequence, unnamed or named.
  • There is no head-restricted blank (Wolfram's _Integer): a wildcard binds to whatever it lands on, with no type filter. Pattern/Optional (x_:default) and condition patterns (_?test) aren't implemented either.

Examples

Basic #
live ↗
InMatchQ(x+1,_a⁡+_b⁡)\mathrm{MatchQ}(x+1, \operatorname{\_a}+\operatorname{\_b})OutTrue\mathrm{True}

Matching is structural: 1+1/φ−φ=01 + 1/\varphi - \varphi = 0, yet it doesn't match 00

live ↗
InMatchQ(1+1φ−φ,0)\mathrm{MatchQ}(1+\frac{1}{\varphi}-\varphi, 0)OutFalse\mathrm{False}
Scope #
live ↗
InMatchQ(xy,_a⁡+_b⁡)\mathrm{MatchQ}(xy, \operatorname{\_a}+\operatorname{\_b})OutFalse\mathrm{False}

__rest (a sequence wildcard) absorbs one or more trailing elements

live ↗
InMatchQ([1,2,3],[_a⁡,__rest⁡])\mathrm{MatchQ}(\bigl\lbrack1, 2, 3\bigr\rbrack, \bigl\lbrack\operatorname{\_a}, \operatorname{\_\_rest}\bigr\rbrack)OutTrue\mathrm{True}
About
Examples5
Checked againstWolfram Language 1 agree1 differ
CrosswalkWolfram Language

See also: FreeQ, Replace