p?test
is a pattern object that stands for any expression that matches p, and on which the application of test gives True.
PatternTest 
p?test
is a pattern object that stands for any expression that matches p, and on which the application of test gives True.
Details
- Any result for test[pval] other than True is taken to signify failure.
- The operator ? has a high precedence. Thus _^_?t is _^(_?t), not (_^_)?t.
- In a form such as __?test, every element in the sequence matched by __ must yield True when test is applied.
- PatternTest has attribute HoldRest.
Examples
open all close allBasic Examples (3)
Find cases of any expression that satisfies the NumberQ test:
Cases[{1, 2, 3.5, x, y, 4}, _ ? NumberQ]Test whether a list has only positive elements:
MatchQ[{1, E, Pi}, {__ ? Positive}]MatchQ[{1, I, 0}, {__ ? Positive}]Create a definition that only applies to numeric arguments:
f[x_ ? NumericQ] := NIntegrate[Sin[t ^ 3], {t, 0, x}]f[2]f[(1 + Sqrt[2]/5)]f[a]Scope (6)
Replace negative numbers with zero:
{3, -5, 2, 7, -6, 3} /. _ ? Negative :> 0Find elements in a list that are divisible by 7:
Cases[Range[0, 70], _ ? (Divisible[#, 7]&)]Elements that are divisible by both 5 and 7:
Cases[Range[0, 350], _ ? (Divisible[#, 7] && Divisible[#, 5]&)]Elements that are divisible by either 5 or 7:
Cases[Range[0, 35], _ ? (Divisible[#, 7] || Divisible[#, 5]&)]Create a function that only evaluates when given a non-negative number and a prime:
f[n_ ? NonNegative, p_ ? PrimeQ] := n^pf[0, 3]f[2, 4]f[-1, 3]Create a function that only evaluates for negative primes:
f[p_ ? (Negative[#] && PrimeQ[#]&)] := p ^ pf[-2]f[2]f[-4]Use PatternTest on a complex pattern:
MatchQ[{{a, b}, {c, d}}, {_, _} ? MatrixQ]MatchQ[{a, b}, {_, _} ? MatrixQ]Search for numbers without allowing expressions to evaluate:
Cases[Hold[Print[5], Sin, 2 + 2, 7, Sqrt[2]], _ ? (Function[{n}, NumberQ[Unevaluated@n], HoldAll])]Just using NumberQ would allow each element to evaluate:
Cases[Hold[Print[5], Sin, 2 + 2, 7, Sqrt[2]], _ ? NumberQ]Properties & Relations (2)
PatternTest applies test functions to patterns, which need not have names:
Cases[{{a, b}, {1, 2, 3}, {{d, 6}, {d, 10}}}, {_, _} ? VectorQ]Condition evaluates a Boolean expression on named parts of a pattern:
Cases[{{a, b}, {1, 2, 3}, {{d, 6}, {d, 10}}}, {x_, y_} /; !ListQ[x] && !ListQ[y]]Use Except to effectively negate PatternTest:
Replace[{1, 7, "Hi", 3, Indeterminate}, Except[_ ? NumericQ] :> 0, 1]Possible Issues (1)
PatternTest evaluates potential matches that are a part of a held expression:
MatchQ[Hold[2 + 3], Hold[_ ? IntegerQ]]Due to normal evaluation rules, expressions may evaluate before being examined by the test function:
MatchQ[Hold[2 + 3], Hold[_ ? (IntegerQ[Unevaluated@#]&)]]Use the three-argument form of Function or write the pattern using Condition to prevent this:
MatchQ[Hold[2 + 3], Hold[_ ? (Function[{n}, IntegerQ[Unevaluated@n], HoldAll])]]MatchQ[Hold[2 + 3], Hold[n_] /; IntegerQ[Unevaluated@n]]Tech Notes
Related Guides
History
Introduced in 1988 (1.0)
Text
Wolfram Research (1988), PatternTest, Wolfram Language function, https://reference.wolfram.com/language/ref/PatternTest.html.
CMS
Wolfram Language. 1988. "PatternTest." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/PatternTest.html.
APA
Wolfram Language. (1988). PatternTest. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/PatternTest.html
BibTeX
@misc{reference.wolfram_2026_patterntest, author="Wolfram Research", title="{PatternTest}", year="1988", howpublished="\url{https://reference.wolfram.com/language/ref/PatternTest.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_patterntest, organization={Wolfram Research}, title={PatternTest}, year={1988}, url={https://reference.wolfram.com/language/ref/PatternTest.html}, note=[Accessed: 12-June-2026]}