This question already has an answer here:

I would like to be extend the below solution to be able to find get all rules from the list step[2] that match the generic pattern k_Digit_p. Where k and p are not generic characters but form part of the symbol name. Unfortunately I've stalled in my understanding of symbolic pattern matches, without converting the symbol names to strings. How can I define the generic pattern above without converting to strings and performing StringMatchQ[] test?

Cases[step[2], PatternSequence[k1p -> _]]

marked as duplicate by Mr.Wizard Jul 25 '15 at 12:44

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.


I don't think you can avoid using strings, but that doesn't mean the output will contain strings.

Pick[step[2], StringMatchQ[ToString /@ step[2][[All, 1]], "k*p"]]
{k1p -> 1.09503, k2p -> 1.32185}
  • $\begingroup$ I see, converting to strings is still extremely fast so that will not be a problem. And having the output as an expression will make testing each element of the returning list of rules straight forward. $\endgroup$ – tarhawk Jul 22 '15 at 14:34

I would use Cases with a condition and SymbolName.

Cases[step[2], (symbol_ -> _) /; StringMatchQ[SymbolName[symbol], "k*p"]]

Another solution:

step[2] /. (symbol_ -> value_) /; Not@StringMatchQ[SymbolName[symbol], "k*p"] :> Nothing

This solution makes use of Nothing which is new in Mathematica version 10.2. Nothing could be replaced by Sequence[] is earlier versions.


If you want to use a pattern, you could use


(* {k1p -> 1.5396, k2p -> 1.91751} *)

Technically this does not convert anything to strings, it just looks up all the user-defined names that match "k*p". It's not really elegant though.

Alternatively you can use Select as well:

Select[step[2], StringMatchQ[ToString[#[[1]]], "k*p"] &]

(* {k1p -> 1.5396, k2p -> 1.91751} *)

Not the answer you're looking for? Browse other questions tagged or ask your own question.