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.
Example:
input:
StringCases["ABCDEFGH",Longest[("D"|"F"|"G")..|("D"|"E"|"H")..|("A"|"B"|"C"|"G")..]]
output:
{"ABC","D","E","FG","H"}
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:
StringCases["ABCDEFGH",Longest[("D"|"E"|"H")..|("D"|"F"|"G")..|("A"|"B"|"C"|"G")..]]
then output is as expected:
{"ABC","DE","FG","H"}
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?