Remove duplicate words with PCRE regex

Added by iNTERFACEWARE

How to remove duplicate words from a string, using PCRE regex with string.rxsub()

Source Code
-- remove duplicate words using rxsub
local s = "hello hello  hello world world"
x = s:rxsub("(\\b\\w+\\b)(\\W+\\1)+","$1") -- by using a word boundary \b PCRE regex
trace(x) --> hello world                   -- can remove multiple repeats (unlike gsub)
Description
How to remove duplicate words from a string, using PCRE regex with string.rxsub()