The apparent problem
The problem here is that you are looking for all letters %a and all punctuation symbols %p, but it stops when it reaches a whitespace, which is %s.
so your code just outputs
Hello
An easy situation would be where the characters that aren't needed are before or after all of the wanted text.
That is where gmatch comes in.
Gmatch
the string.gmatch function is a pretty useful one, as it is an iterator. That meaning i can do this:
local str = "Hi My Name Is Bob"
for word in string:gmatch(%a+) do
print(word)
end
For your purpose, you can simple get the word by doing
String = " Hello $&@ World !"
for word in String:gmatch("%a+") do
print(word)
end
or
String = " Hello $&@ World !"
for word in String:gmatch("[%p%s]+") do
print(word)
end
And then you can simply add them to a table and concatenate them
String = " Hello $&@ World !"
local words = {}
for word in String:gmatch("[^%a]+") do
table.insert(words,word)
end
print(table.concat(words," "))
However, the exclamation point can't be used as $,&, and @ are also considered punctuation characters, which means you could also be included :/
Reference links:
gmatch
patterns and "magic characters"
Hopefully this helped you out :3