Skip to content

Instantly share code, notes, and snippets.

@rameshvarun
Last active December 17, 2015 23:19
Show Gist options
  • Save rameshvarun/5688388 to your computer and use it in GitHub Desktop.
Save rameshvarun/5688388 to your computer and use it in GitHub Desktop.
Simple configuration file parser, using a regex.
configfile = [[
ip = 127.0.0.1
tcp port = 10
udp port = 11
lan only = false
]]
config_regex = "%s*(.-)%s*=%s*(.-)%s*\n"
function parseString( data )
local result = {}
for key, value in configfile:gmatch(regex) do
if not (tonumber( value ) == nil) then
result[key] = tonumber( value )
elseif value:lower() == "false" then
result[key] = false
elseif value:lower() == "true" then
result[key] = true
else
result[key] = value
end
end
return result
end
for key, value in pairs( parseString(configfile) ) do print(key, value) end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment