Ignore all punctuation in a string

Hey guys, reaching out for your wizardly skills to help me with a small issue.

Trying to come up with a string pattern to remove all punctuation from a string, for example:

  • From: TE@ST :STR@ING
  • To: TEST STRING

I’ve read through http://robloxdev.com/articles/string-patterns-reference and can’t come up with a solution

Tried [%a%p]+, [^p]+%a and many other combinations
So far I can get it to ignore all in the first word but stop there and not continue to the second, or any more.

Any point in the right direction would be very helpful, thanks!

1 Like
local myString = "TE#ST :STR@ING"
local newString = string.gsub(myString, "%p", "")
print(newString)

Output:

TEST STRING

There’s an example I put together. I’m not experienced with string patterns, but this should work from what I can tell.

See this reference for info on gsub.

4 Likes

Of course it’s that simple, thank you :slight_smile:

Another way to think about this is a series of captures where the delimiters are the punctuation.

For example (it might be a little bit longer than the gsub method) but you can do something along the following:

local InputString = "TE'ST;STr!NG?"
local OutputString = ""

for i in string.gmatch(InputString,"%p*(%w+)%p*") do
    OutputString = OutputString..i
end
1 Like