S
S
Stergy2019-07-12 01:39:39
ruby
Stergy, 2019-07-12 01:39:39

How to remove duplicate words in a string in ruby?

A certain line comes to me, I need to remove duplicate words if they are nearby, and the registers may differ, you always need to delete the second word.
example:

Hello Hello my name Name Is is John john yes Yes John

should work
Hello my name Is John yes John
. How can I implement this with Ruby?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-07-12
@Stergy

str.gsub(/\b(\w+) \1/i, '\1')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question