Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

According to the documentation,

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

returns

{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.

share|improve this question
1  
StringCases["string", #] & /@ {p1, p2} –  Kuba Jul 9 at 21:37
    
@Kuba: Looks good, thanks. –  DumpsterDoofus Jul 9 at 23:29

1 Answer 1

up vote 1 down vote accepted

Maybe

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"} *)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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