I have the following list:


I want to modify it to have the following:


Two things happen in this process: 1. "Food." shall be added to the beginning of each entry, 2. The first word of each entry shall be added to the middle. For example when we have "ALMONDBEAN" we want to get "ALMOND." and eventually have "Food.ALMOND.ALMONDBEAN"

reArrange[s_String] := ## & @@ StringCases[s, 
   StartOfString ~~ w__ ~~ rest__ ~~ EndOfString /; 
     (And @@ DictionaryWordQ /@ {w, ## & @@ StringSplit[rest, "_"]}) :> 
       StringRiffle[{"Food", w, s}, "."]]

reArrange /@ l



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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