4
$\begingroup$

I have the following text:

text = {"hello","hello","hello","c","a","e","e","hello","hello"}

Now I would like to do the following:

Delete every string that is "hello" and repeats itself consecutively.

Result:

text = {"hello","c","a","e","e","hello"}
$\endgroup$
4
$\begingroup$

For version 11.3:

SequenceReplace[text, {"hello" ..} -> "hello"]

One approach for the versions older than 11.3:

Split[text] /. {"hello" ..} -> "hello" // Flatten
$\endgroup$
  • $\begingroup$ Very nice ! Thanks a lot ! :) $\endgroup$ – james Oct 16 '18 at 6:22
  • $\begingroup$ @james With pleasure :). $\endgroup$ – Αλέξανδρος Ζεγγ Oct 16 '18 at 6:25
3
$\begingroup$

Also

ReplaceRepeated[text, {a___, Longest["hello" ..], b___} :> {a, "hello", b}]

{"hello", "c", "a", "e", "e", "hello"}

$\endgroup$
  • $\begingroup$ This also works. But why some codes (a___, Longest, b___) in my notebook turn red? $\endgroup$ – Αλέξανδρος Ζεγγ Oct 16 '18 at 6:28
  • $\begingroup$ @ΑλέξανδροςΖεγγ, i don't get that in version 9 or version 11.3. $\endgroup$ – kglr Oct 16 '18 at 6:30

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.