Last active
December 17, 2015 23:19
-
-
Save rameshvarun/5688388 to your computer and use it in GitHub Desktop.
Simple configuration file parser, using a regex.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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