Removing repeated words in a string

Post your questions and help other users.

Moderator: Martin

Post Reply
Wibbly
Posts: 418
Joined: 17 Mar 2014 09:02

Removing repeated words in a string

Post by Wibbly » 09 Jan 2020 18:20

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"

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Removing repeated words in a string

Post by Martin » 09 Jan 2020 20:48

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

Wibbly
Posts: 418
Joined: 17 Mar 2014 09:02

Re: Removing repeated words in a string

Post by Wibbly » 10 Jan 2020 09:30

Thanks for the steer Martin

Post Reply