First you need a string pattern that matches your inputs. The first part of your string "A Big Cheese"
, can be matched by two underscores, __
. The last part is always three upper case letters in a row. You can match any upper case letter with
caps = Alternatives @@ (ToUpperCase /@ Alphabet[])
You can see it work here,
StringMatchQ["A", caps]
(* True *)
To catch three capital letters in a row, you can join them using ~~
StringMatchQ["ABC", caps ~~ caps ~~ caps]
(* True *)
StringMatchQ["AbC", caps ~~ caps ~~ caps]
(* False *)
Or you can use Repeated
,
StringMatchQ["XYZ", Repeated[caps, {3}]]
(* True *)
Now that we have our pattern we can give it to StringCases
. You need to name the patterns and apply a transformation rule
StringCases["A Big Cheese ABC", a__ ~~ b : Repeated[caps, {3}] :> {a, b}]
(* {{"A Big Cheese ", "ABC"}} *)
You can wrap this up into a function, using First
to remove the extra curly brackets,
splitSTRING["examine your zipper XYZ"]
(* {"examine your zipper ", "XYZ"} *)
Edit
As march points out you don't have to use Alternatives
to make a Pattern
, you could use UpperCaseQ
to make a PatternTest
:
StringCases["A Big Cheese ABC",
a__ ~~ b : Repeated[_?(UpperCaseQ), {3}] :> {a, b}]
(* {{"A Big Cheese ", "ABC"}} *)
StringPattern
and feed it toStringReplace
$\endgroup$str
is not a string. $\endgroup$