Still have questions? Join our Discord server and get real time help.
0

# Include punctuation when using string.gmatch()?

Ghusoc 50
14 days ago

Hello,

I'm working on this project, and it kind of emulates a small part of TF2's economy, where items will have a quality. At some point in the game, the item is eventually renamed to add the quality as a prefix, e.g. Emerald Eye becomes Ordinary Emerald Eye.

I managed to whip up a small helper function that successfully removes the prefix, but I'm not particularly well versed in string manipulation.

Here's the code:

function getOriginalName( name )
local words = {}
for word in name:gmatch("%w+") do
table.insert(words, word)
end
table.remove(words, 1)
return table.concat(words, " ")
end


Now, if I used Ordinary Emerald Eye for example, it successfully removes the Ordinary prefix. However, if I use an item that has some punctuation in the name, e.g. Ordinary Mr. Hatbot, it doesn't quite return what I need, and therein lies my issue. It will return the name, excluding the period.

Would anyone be able to assist me in this? I tried editing the pattern, so it was %wp+ rather than %w+, but to no avail.