Still have questions? Join our Discord server and get real time help.
Log in to vote

Include punctuation when using string.gmatch()?

Asked by
Ghusoc 50
14 days ago


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)
    table.remove(words, 1)
    return table.concat(words, " ")

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.

Cheers in advance!

%p ? incapaxx 3270 — 14d

Answer this question