According to the documentation,

StringCases[{s1, s2, ...}, p]


{StringCases[s1, p], StringCases[s2, p], ...}

but, oddly enough,

StringCases["string", {p1, p2, ...}]

returns the results which match any of the patterns $p_i$, which is essentially equivalent to

Union[{StringCases["string", p1], StringCases["string", p2], ...}]

Is there a way to modify StringCases["string", {p1, p2, ...}] to return just

{StringCases["string", p1], StringCases["string", p2], ...}

in an analogous manner to how StringCases[{s1, s2, ...}, p] does it?

One alternate way to get the same output would be

Through[Table[StringCases[#, p[i]] &, {i, n}]["string"]]

but I was curious if there was another way.

  • 1
    $\begingroup$ StringCases["string", #] & /@ {p1, p2} $\endgroup$ – Kuba Jul 9 '14 at 21:37
  • $\begingroup$ @Kuba: Looks good, thanks. $\endgroup$ – DumpsterDoofus Jul 9 '14 at 23:29


p = {"abc", "cd"};
string = "abcdabcdcd";

StringCases @@@ Thread[{string, p}]
(* {{"abc", "abc"}, {"cd", "cd", "cd"}} *)

Original post:

StringCases[string, p, Overlaps -> All]
(*  {"abc", "cd", "abc", "cd", "cd"} *)
|improve this answer|||||

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.