lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> Well, it's the middle of the night and I can't sleep, but this passes
> all your tests, and all other tests posted in the thread so far
> (except "$undefined" expands to "" rather than "$undefined", as you
> requested):
> 
> local function expand_variables(str)
>    return (str:gsub("%$([_%w]+)", "${%1}"):gsub("%${([_%w]+)}", function (v)
>         return variables[v] or "";
>    end));
> end

That would be my answer, too, except that I would use "%b{}" in the
second gsub. (That changes a little the meaning; it allows arbitrary
"names" inside the brackets, as long as they have balanced brackets.)

-- Roberto