Some weeks ago, I posted a question which involved creating string pairs from a list of string elements. One of the answers posted (by Kuba) worked very well until I found an example where it didn't. Here it is:
lis = {"ABCDEFGHI" | "JKLMN OP" | "PQR" | "JKLMN" | "OPXYZ"};
prs = {"JKLMN OP ABCDEFGHI", "JKLMN OPXYZ", "PQR JKLMN"};
Given the list prs whose elements consist of two members of lis separated by a space, the task is to create a modified prs where the elements of prs are separated into two elements that are members of lis.
Kuba's suggestion was essentially:
separatedNames = Table[StringSplit[prs[[i]]], {i, Length[prs]}];
alternative = Apply[Alternatives]@Reverse@SortBy[StringLength]@lis[[1]];
res = Partition[Flatten[StringCases[alternative] /@ StringRiffle /@ separatedNames],2]
This produces: {{"JKLMN OP", "ABCDEFGHI"}, {"JKLMN OP", "PQR"}}
and not the desired: {{"JKLMN OP","ABCDEFGHI"},{"JKLMN","OPXYZ"},{"PQR","JKLMN"}}
As always thanks for thoughts.