Remove duplicate words with PCRE regex

Verified
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()
Usage Details

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

How to use the snippet:

  • Paste the code into your script
  • Inspect the annotations to see how it works