Why doesn't
Cases[{"a", "b"}, "a"~~___]
return {"a"} instead of {}?
I used three underscores. More detailed, why does 62 work, but 61 not
In Cases[{"abcde", "b"}, "a"~~__] Out[61]= {} In[62]:= StringMatchQ["abcde", "a" ~~ __] Out[62]= True
Paul,
You need either
StringCases[{"a", "b"}, "a" ~~ ___]
which returns
{{"a"}, {}}
or
Select[{"ah", "b", "a"}, StringMatchQ["a" ~~ ___]]
which gives what I think you want:
{"ah", "a"}
I do not believe that Cases can take a string pattern. For that you would need to use StringCases.
Regards
As Neil also said, you are mistaking regular patterns with string patterns. Cases takes a pattern (which can involve strings!), StringMatchQ takes string patterns.
Look at the documentation:
guide/Patterns tutorial/StringPatterns
In your case you can also use:
Select[{"a", "b"}, StringStartsQ["a"]]
Duplicate MMa.SE thread with good discussion on the subject: