0
$\begingroup$

This question already has an answer here:

The title refers to the general problem of writing patterns to match pattern-containing expressions (hence "meta-match").

Here's an example.

In the following rules

0 -> "A"
foo[1] -> bar[2]
{1, 10} -> 11

the left-hand-sides contain no patterns, at least explicitly. I'll refer to such rules as being "pattern-free".

Now, given a list of rules (rules), how can one obtain the sublist consisting only of the pattern-free ones?

This naive solution won't work:

Cases[rules,x_ /; FreeQ[x, (Blank[___] | BlankSequence[___] | BlankNullSequence[___])]]

I can solve this problem at least with the following hack

Cases[(rules /. (Blank | BlankSequence | BlankNullSequence) -> $patt),
      y_ /; FreeQ[y, $patt[___]]]

or

Cases[(rules /. x:(Blank | BlankSequence | BlankNullSequence) -> $patt[x]),
      y_ /; FreeQ[y, $patt[_][___]]
] /. $patt[z_] -> z

...but I figure that Mathematica may provide standard constructs to deal with such situations. If so, I'd like to learn what they are.

$\endgroup$

marked as duplicate by Mr.Wizard Jan 18 '15 at 15:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 5
    $\begingroup$ Verbatim and PatternHold are useful. $\endgroup$ – Szabolcs Jan 16 '15 at 15:07
  • 2
    $\begingroup$ What @Szabolcs said. Except the second one should be HoldPattern. $\endgroup$ – Daniel Lichtblau Jan 16 '15 at 15:44
  • $\begingroup$ Related: (2778), (17591), (17892), (38436), (43972) $\endgroup$ – Mr.Wizard Jan 16 '15 at 18:36
  • $\begingroup$ How would you owant to deal with expressions with things like x[Verbatim[2/;True]] or x[HoldPattern[2]]? $\endgroup$ – Rojo Jan 16 '15 at 20:53

Browse other questions tagged or ask your own question.