Question about string.gsub

local function pretty_date(date)
    local dateString = "{year}-{month}-{day} {hour}:{min}"
    local result = string.gsub(dateString, "{(%w+)}", date)
    return result
end

-- the time I wrote this was May 18, 2018 11:48 AM PDT, these values will obviously be different.
local now = os.time() -- roughly 1526668592
print(pretty_date(os.date("*t", now))) -- 2018-5-18 11:48, this is the local time
print(pretty_date(os.date("!*t", now))) -- 2

I found this code on the DevForum and it works fine, but what does the “%w+” in line 3 mean? Or the *t in line 9 or the !*t in line 10.

. all characters
%a letters
%c control characters
%d digits
%l lower case letters
%p punctuation characters
%s space characters
%u upper case letters
%w alphanumeric characters
%x hexadecimal digits
%z the character with representation 0

taken from lua’s official documentation: Programming in Lua : 20.2

1 Like

Thank you, thats exactly what I was looking for.

1 Like

%w+, in the context of string patterns, will search for 1 or more repetitions of consecutive alphanumeric characters (meaning both letters, numbers and other characters).

For example:

local str = "Hi roblox1234....."
print( str:match("%w+") ) -- it just returns "Hi", since between the text is whitespace which isn't considered an alphanumeric character

*t is used in the os.date function as a format string to return a table containing information about the date and time at that particularpoint in time that the machine the code is running on would have if it was at that specific point in time