I want to partition string into longest substrings that each contain only specific characters, beginning from left to right with no overlaps, always choosing the longest one possible at current position. In my example only substrings that contain only characters d,f,g or d,e,h or a,b,c,g are allowed.






But after "ABC" there is evidently substring "DE" that is longer than "D" or "E". So my expected output would be:{ABC,DE,FG,H}

If I switch first and second argument of Alternatives this way:


then output is as expected:


But Alternatives should be from definition something that is independent on arguments order. So I would expect in both inputs the same output (second one).

So my question is how to do it that I get always longest possible substring no matter what order of arguments inside Alternatives is?


2 Answers 2


You could do:

    Longest[p__] /; StringMatchQ[p,("D"|"F"|"G")..|("D"|"E"|"H")..|("A"|"B"|"C"|"G")..]

{"ABC", "DE", "FG", "H"}

  • $\begingroup$ Yes, works as I wanted. But I hoped for a more efficient method. I can do it much faster using two nested While loops, taking character by character till all of them belong to one set of characters. Anyway, I accept your answer. $\endgroup$ Commented Feb 28, 2019 at 11:10

But Alternatives should be from definition something that is independent on arguments order.

StringCases is based on the PCRE regular expression engine for which it isn't true: a regex engine always returns the first match from listed in alternation (when it allows to match the whole pattern).

To get the behavior you expected you should use SequenceCases instead (which doesn't use regexes and is based on Mathematica's own pattern-matcher):

StringJoin @@@ 
      {Longest[("D" | "F" | "G") .. | ("D" | "E" | "H") .. | 
               ("A" | "B" | "C" | "G") ..]}]
{"ABC", "DE", "FG", "H"}

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.