1
$\begingroup$

Thank you all for your suggestions for my previous question - very helpful indeed. Here is a related but slightly different problem and I'd be grateful for input.

I have a series of strings that end with a space followed by 2, 3, or 4 capital letters:

 {A Big Cheese ABC}
 {Does Nothing DN}
 {for My Dental Health FMDH}
 {Insurance (IN) INS}

I'd like to split the strings to get:

 {A Big Cheese, ABC}
 {Does Nothing, DN}
 {for My Dental Health, FMDH}
 {Insurance (IN), INS}

This would seem to require a different Replace rule, as the all-caps segment of the strings are now of different lengths; the rule would also need to ignore consecutive caps when not at the end of the string, as in the fourth. Again thanks in advance for ideas.

$\endgroup$
2
  • 2
    $\begingroup$ StringSplit[#, " " ~~ b : CharacterRange["A", "Z"] .. ~~ EndOfString :> b] & $\endgroup$
    – wxffles
    Oct 13, 2016 at 3:25
  • $\begingroup$ Works perfectly. Thanks! $\endgroup$
    – Suite401
    Oct 13, 2016 at 18:42

1 Answer 1

0
$\begingroup$

Here is a method that works for all cases including when the last word is not capitalized using RegularExpression

data = {"A Big Cheese ABC", "A Big Cheese abc", "Does Nothing DN", 
"for My Dental Health FMDH", "Insurance (IN) INS"};

StringSplit[#, b__ ~~ " " ~~ patt : (RegularExpression["[A-Z]+"] ~~ EndOfString) :> 
Sequence[b, patt]] & /@ data

(* {{"A Big Cheese", "ABC"}, {"A Big Cheese abc"}, {"Does Nothing", 
"DN"}, {"for My Dental Health", "FMDH"}, {"Insurance (IN)", "INS"}} *)
$\endgroup$

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