Skip to content

HoldPattern

HoldPattern[expr] leaves expr unevaluated but is seen as just expr for the purposes of pattern matching.

Attributes[HoldPattern] := {HoldAll, Protected}

Simple examples

In[1]:= MatchQ[2*x + 2*y, 2*_ + 2*_]
Out[1]= False
In[2]:= MatchQ[2*x + 2*y, HoldPattern[2*_ + 2*_]]
Out[2]= True
In[3]:= MatchQ[2*x + 2*y, HoldPattern[2*_ + HoldPattern[2*_]]]
Out[3]= True