I would like to select nested lists out of a master list based on certain string pattern as in the following example:

lis = {{a, b, c, d}, {xy, y, z, w}, {e, f, g, h}, {xz, f, g, h}};

I would like to create a list with all nested lists starting with x* in the fist position.

I tried the following functions:

Cases[lis, {q_, r__} /; StringMatchQ[q, "x*"] -> {q, r}]


Select[lis2, First[#] == x &]

Both do not work.

Does anyone can give me a hint?


Just get the symbol's name and match for that:

Cases[lis, {q_, r__} /; StringMatchQ[SymbolName[q], "x*"] -> {q, r}]
(* {{xy, y, z, w}, {xz, f, g, h}} *)

or using Select:

Select[lis, First@Characters[SymbolName[First@#]] == "x" &]
  • $\begingroup$ Thanks, but it still does not work. Obviously my question was incorrect. I have a list of all stock indicies available in Mathematica. Some of these entries are obviously no indices Therefore I would like to select only the "complete" entries using "^*" as identifier . The list looks as follows: lis = {{"^DYI", "Dynamic Market Intellidex Index", "AMEX", "Dynamic Market \ Intellidex Index"}, {FinancialData["^EBT-TC", "Symbol"], {"^EEV-NV", "Ishares Msci Emerging Markets I", "AMEX", \ "Ishares Msci Emerging Markets I"}} Any idea? Thanks Phadreus $\endgroup$ – Phadreus Mar 9 '15 at 12:13
  • $\begingroup$ Thanks, I solved the problem. I missed the String definition of the variable q_String. Here my final function: Cases[lis, {q_String, r__} /; StringMatchQ[q, "^*"] -> {q, r}] $\endgroup$ – Phadreus Mar 9 '15 at 13:49
  • $\begingroup$ At least I pointed you in the right direction. :) $\endgroup$ – Jinxed Mar 9 '15 at 14:25
  • $\begingroup$ @Phadreus: And, I hope, my answer is actually an answer to your original question. If so, would you mind marking it as such? $\endgroup$ – Jinxed Mar 9 '15 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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