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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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