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$
8
  • 2
    $\begingroup$ How do you determine where to insert the :? The example is not self-explanatory. $\endgroup$
    – Szabolcs
    Commented Jan 21, 2019 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
    Commented Jan 21, 2019 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
    Commented Jan 21, 2019 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
    Commented Jan 21, 2019 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
    Commented Jan 21, 2019 at 14:25

1 Answer 1

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 and acknowledge you have read our privacy policy.

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