Skip to content

PatternTest

pat?test matches when the expression matches pat and test[MATCH] evaluates to True.

Attributes[PatternTest] := {HoldRest, Protected}

Simple examples

In[1]:= MatchQ[1, PatternTest[_, NumberQ]]
Out[1]= True
In[2]:= MatchQ[a, PatternTest[_, NumberQ]]
Out[2]= False
In[3]:= MatchQ[1, PatternTest[1, NumberQ]]
Out[3]= True
In[4]:= MatchQ[1, PatternTest[1.5, NumberQ]]
Out[4]= False
In[5]:= MatchQ[1.5, PatternTest[1.5, NumberQ]]
Out[5]= True
In[6]:= Cases[{5, 2, a^b, x, y, 4.5}, PatternTest[_, NumberQ]]
Out[6]= {5, 2, 4.5}