There is, say, the following line:

string = 'Word Word Word something else Word' 

Using regular expressions, how to bring this string to the form:

 string = 'Word something else Word' 

That is, words repeating once do not change, and those words that occur several times in succession, delete and leave only one of a number of repetitive ones.

  • show your solution - Mikhail Vaysman
  • There are lots of questions about working with repetitions and they are even with ready-made answers ... ru.stackoverflow.com/… - Mike
  • And if there is a string = 'Word,Word; Word something else Word' string = 'Word,Word; Word something else Word' ? There are many special cases. - Wiktor Stribiżew
  • Possible duplicate question: How to remove repeated words using a regular expression? - Wiktor Stribiżew

1 answer 1

You can push off from this

 string = 'Word Word Word something else Word' r = %r{(?<word>\w+) +(?=\k<word>)} puts string.gsub(r, '') 

here ?<word> is a named group, and ?= look ahead with return

  • And if I need to exclude not all duplicate words in a line, but a specific word (word or any other), and do not touch the remaining duplicate words, how to be? that is, the string: string = 'Word Word Word something else else Word', and get the output: string = 'Word something else else Word', - onetwothree
  • one
    just notice the search for words from any letters on a specific word: r = %r{(?<word>(Word)) +(?=\k<word>)} - Evgeny Yakovchuk
  • Thank you! Very rescued - onetwothree