0
$\begingroup$

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}]

or

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

Both do not work.

Does anyone can give me a hint?

$\endgroup$
2
  • $\begingroup$ Are those strings in lis or you just want to use string patterns for symbols? $\endgroup$
    – Kuba
    Commented Mar 9, 2015 at 8:44
  • 2
    $\begingroup$ For the latter case I'd say it is a duplicate: Pattern matching multiple symbols names and powers $\endgroup$
    – Kuba
    Commented Mar 9, 2015 at 8:47

1 Answer 1

3
$\begingroup$

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" &]
$\endgroup$
4
  • $\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
    Commented Mar 9, 2015 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
    Commented Mar 9, 2015 at 13:49
  • $\begingroup$ At least I pointed you in the right direction. :) $\endgroup$
    – Jinxed
    Commented Mar 9, 2015 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
    Commented Mar 9, 2015 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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