0
$\begingroup$

Suppose we have a list of the following:

l={"applejuice","lemon_juiceginger"}

How can one modifies this to read as:

{"apple:juice","lemon_juice:ginger"}

I have tried StringRiffle[] but wasn't so successful.

$\endgroup$
  • 2
    $\begingroup$ How do you determine where to insert the :? The example is not self-explanatory. $\endgroup$ – Szabolcs Jan 21 '19 at 14:19
  • $\begingroup$ after each known word, apple is known so as juice. problem is with enteries like lemon_juice where there is "_". $\endgroup$ – Wiliam Jan 21 '19 at 14:21
  • $\begingroup$ Assuming you need to detect and split words, have you tried any techniques from answers to your recent question: mathematica.stackexchange.com/q/189303/5478? $\endgroup$ – Kuba Jan 21 '19 at 14:21
  • $\begingroup$ @William what kind of problem because "after each known word" means that "lemon_juiceginger" should be "lemon:_juice:ginger:". $\endgroup$ – Kuba Jan 21 '19 at 14:22
  • $\begingroup$ @Kuba I have tried the method I asked in previous post. The "_" is the reason I am asking this here. Let me be specific. the ":" should go after the first known word. One expects "apple:juice" which is fine. Yet when there is "lemon_juiceginger" you get "lemon:_juiceginger" how would one sort the string such that it understand lemon_juice is one word? $\endgroup$ – Wiliam Jan 21 '19 at 14:25
1
$\begingroup$

We can use a slight modification of the function reArrange from this answer:

ClearAll[reArrange2]
reArrange2[s_String] := ## & @@ 
  StringCases[s, StartOfString ~~ beg__ ~~ w__ ~~ EndOfString /; 
    (And @@ DictionaryWordQ /@ {w, ## & @@ StringSplit[beg, "_"]}) :> 
     StringRiffle[{beg, w}, ":"]]

reArrange2 /@ l

{"apple:juice", "lemon_juice:ginger"}

$\endgroup$

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.