Skip to content

Instantly share code, notes, and snippets.

@badcc
Created July 19, 2015 21:50
Show Gist options
  • Save badcc/88249ee5584c57ba37e0 to your computer and use it in GitHub Desktop.
Save badcc/88249ee5584c57ba37e0 to your computer and use it in GitHub Desktop.
local Tests = {
'<string name="Name">TEST</string>', -- pass
'<>TEST</string>', -- load
'< >TEST</string>', -- load
'<a a>TEST</string>', -- fail = not found
'<string a=" name="Name">TEST</string>', -- fail = not found
'<string a= a=""="">TEST</string>', -- ??? load
'<string a= ">TEST</string>', -- ??? load
'<string a= a=""="=>TEST</string' -- fail " not found
}
local function Parse(Str)
local OpenFirstTagMatch = '^<%s*%w*%s*([^>]*)'
local PropertyMatch = '(%w+)(=?"?[^"]*"?)'
local CloseFirstTagMatch = '%s*>'
local ValueMatch = '>(.+)<'
local SecondTagMatch = '</(%w+)>'
local Properties = Str:match(OpenFirstTagMatch)
for PropertyName, PropertyValue in Properties:gmatch(PropertyMatch) do
if (PropertyValue:sub(1, 1) ~= '=') then
return 'fail (\'=\' not found)'
end
if (not PropertyValue:match('"[^"]+"')) then
return 'fail (\'"\' not found)'
end
end
local Value = Str:match(ValueMatch)
if (not Value) then
return 'fail'
end
local SecondTagName = Str:match(SecondTagMatch)
if (not SecondTagName) then
return 'fail'
end
if (Value == 'TEST' and #Properties > 0) then
return 'pass'
end
return 'load'
end
for _,Str in next,Tests do
local Result = Parse(Str)
print(Result)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment