The Verbatim usage message is shown below.

? Verbatim

Verbatim[expr] represents expr in pattern matching,   requiring that expr be matched exactly as it appears, with no substitutions   for blanks or other transformations. More…

Verbatim[expr] is used when you want the pattern matcher to ignore the normal
meaning of a pattern matching feature.  To demonstrate the next cell shows
the FullForm of some patterns.

FullForm[{x_, y_List}]

List[Pattern[x, Blank[]], Pattern[y, Blank[List]]]

The attempt to delete all such patterns in the next cell fails because the
pattern matcher treats the use of  "Pattern" here as a pattern matching
construct instead of an actual form to search for.

DeleteCases[{x_, y_List, 3, 4, 5}, Pattern[_, _Blank]]

{x_, y_List, 3, 4, 5}

In the next cell the pattern matcher knows to ignore the meaning of "Pattern
" so (x_) and (y_List) are deleted.  In this case only Pattern was wrapped
in Verbatim so the pattern matcher did consider the meaning of (_) and

DeleteCases[{x_, y_List, 3, 4, 5}, Verbatim[Pattern][_, _Blank]]

{3, 4, 5}

In the next example (Pattern[_,_Blank]) is wrapped in Verbatim so only that
literal expression is deleted.

data = {x_, y_List, 3, 4, 5, Pattern[_, _Blank], 6, 7} ;  DeleteCases[data, Verbatim[Pattern[_, _Blank]]]

{x_, y_List, 3, 4, 5, 6, 7}

Suppose you're given a list and want to get the element that matches the  pattern (opt1→_).  The attempt in the next cell doesn't work because  use of a rule as the second argument in Cases has special meaning.

Cases[{1 + x, opt1val, opt2val2, opt1}, opt1_]


In the next cell Verbatim makes Cases treat it's second argument as a form to
search for instead of replacing (opt1) with (_).

Cases[{1 + x, opt1val1, opt2val2, opt1}, Verbatim[Rule][opt1, _]  ]


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page