Page 1 of 1

Removing repeated words in a string

Posted: 09 Jan 2020 18:20
by Wibbly
Anyone know how to write an expression to remove the second instance of repeated words where they exist in a string?

Suppose string1 = "It's going to rain in 10mins in London London". I want string2 = "It's going to rain in 10mins in London"

Re: Removing repeated words in a string

Posted: 09 Jan 2020 20:48
by Martin
Check out this page:
https://stackoverflow.com/questions/282 ... cate-words

Something like this should work:

Code: Select all

string2 = replaceAll(string1, '(\\b\\S+\\b)\\s+\\b\\1\\b', '$1');
Regards,
Martin

Re: Removing repeated words in a string

Posted: 10 Jan 2020 09:30
by Wibbly
Thanks for the steer Martin