({{, p?test
is a pattern object that stands for any expression that matches p, and on which the application of test gives (RefLink[True, paclet:ref/True]). }})

OpenersNotesSection(Spacer1)Details

Any result for test[pval] other than (RefLink[True, paclet:ref/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 (RefLink[True, paclet:ref/True]) when test is applied.

(RefLink[PatternTest, paclet:ref/PatternTest]) has attribute (RefLink[HoldRest, paclet:ref/HoldRest]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (12)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

Find cases of any expression that satisfies the (RefLink[NumberQ, paclet:ref/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]

OpenersExampleSection1(Spacer1)Scope  (6)

Replace negative numbers with zero:

{3,-5,2,7,-6,3}/._?Negative:>0

Find 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^p)

f[0,3]

f[2,4]

f[-1,3]

Create a function that only evaluates for negative primes:

f[p_?(Negative[#] && PrimeQ[#]&)]:= p^p

f[-2]

f[2]

f[-4]

Use (RefLink[PatternTest, paclet:ref/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 (RefLink[NumberQ, paclet:ref/NumberQ]) would allow each element to evaluate:

Cases[Hold[Print[5], Sin, 2+2, 7, (Sqrt[2])], _?NumberQ]

OpenersExampleSection2(Spacer1)Properties & Relations  (2)

(RefLink[PatternTest, paclet:ref/PatternTest]) applies test functions to patterns, which need not have names:

Cases[{{a,b}, {1,2,3}, {{d,6},{d,10}}}, {_,_}?VectorQ]

(RefLink[Condition, paclet:ref/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 (RefLink[Except, paclet:ref/Except]) to effectively negate (RefLink[PatternTest, paclet:ref/PatternTest]):

Replace[{1,7,"Hi",3,Indeterminate},Except[_?NumericQ]:>0,1]

OpenersExampleSection3(Spacer1)Possible Issues  (1)

(RefLink[PatternTest, paclet:ref/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 (RefLink[Function, paclet:ref/Function]) or write the pattern using (RefLink[Condition, paclet:ref/Condition]) to prevent this:

MatchQ[Hold[2+3],Hold[_?(Function[{n},IntegerQ[Unevaluated@n],HoldAll])]]

MatchQ[Hold[2+3], Hold[n_] /; IntegerQ[Unevaluated@n]]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Condition, paclet:ref/Condition])  ▪  (RefLink[Element, paclet:ref/Element])  ▪  (RefLink[Except, paclet:ref/Except])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Putting Constraints on Patterns, paclet:tutorial/PuttingConstraintsOnPatterns])}, {▪(RefLinkPlain[String Patterns, paclet:tutorial/StringPatterns])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Patterns, paclet:guide/Patterns])}, {▪(RefLinkPlain[Wolfram Language Syntax, paclet:guide/Syntax])}})}})