MatchQ
Examples
open all close allBasic Examples (3)
Test if an expression is an integer:
MatchQ[12345, _Integer]Test if an expression is a sum of two or more terms:
MatchQ[(x - 1)(1 + 2 x + 3 x ^ 2), Plus[_, __]]MatchQ[Expand[x(1 + 2 x + 3 x ^ 2)], Plus[_, __]]Test if an expression is explicitly zero:
MatchQ[1 + 1 / GoldenRatio - GoldenRatio, 0]MatchQ[Simplify[1 + 1 / GoldenRatio - GoldenRatio], 0]Scope (10)
MatchQ[<|a -> 1, b -> 2|>, _Association]Match a rule in an association, using an association as a pattern:
MatchQ[<|a -> 1|>, <|a -> _|>]MatchQ[<|a -> 1|>, <|_ -> 1|>]KeyValuePattern lets you match any element in an association:
MatchQ[<|a -> 1, b -> 2, c -> 3|>, KeyValuePattern[b -> 2]]It also works on lists of rules:
MatchQ[{a -> 1, b -> 2, c -> 3}, KeyValuePattern[b -> 2]]Match a rule in an association with a condition:
MatchQ[<|a -> 1|>, <|a -> _ ? OddQ|>]MatchQ[<|a -> "foo"|>, <|a -> x_ /; StringQ[x]|>]Match a Rule in an association using Alternatives:
MatchQ[<|a -> 1|>, <|_ -> 1 | 2|>]Use Verbatim to match a literal blank:
MatchQ[<|a -> _|>, <|a -> Verbatim[_]|>]_ or Blank has special meaning in an association used as a pattern:
MatchQ[<|a -> 1|>, <|a -> _|>]Use __ or BlankSequence to match more than one rule in an association:
MatchQ[<|a -> 1, b -> 2|>, <|_|>]MatchQ[<|a -> 1, b -> 2|>, <|__|>]Match an association containing a given rule and possibly more:
MatchQ[<|a -> 1|>, <|a -> 1, ___|>]MatchQ[<|a -> 1, b -> 2|>, <|a -> 1, ___|>]Match an association containing three elements:
MatchQ[<|a -> 1, b -> 2, c -> 3|>, <|_, _, _|>]MatchQ[<|a -> 1, b -> 2, c -> 3|>, a_ /; Length[a] == 3]MatchQ[<|1 -> <|2 -> x|>|>, <|_ -> <|_|>|>]See Also
StringMatchQ FreeQ MemberQ Replace Pattern
Function Repository: PatternBindings CheckMatch ASTPattern
Tech Notes
Related Guides
Related Links
History
Introduced in 1988 (1.0) | Updated in 2014 (10.0)
Text
Wolfram Research (1988), MatchQ, Wolfram Language function, https://reference.wolfram.com/language/ref/MatchQ.html (updated 2014).
CMS
Wolfram Language. 1988. "MatchQ." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/MatchQ.html.
APA
Wolfram Language. (1988). MatchQ. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/MatchQ.html
BibTeX
@misc{reference.wolfram_2026_matchq, author="Wolfram Research", title="{MatchQ}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/MatchQ.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_matchq, organization={Wolfram Research}, title={MatchQ}, year={2014}, url={https://reference.wolfram.com/language/ref/MatchQ.html}, note=[Accessed: 12-June-2026]}