1
0
mirror of https://github.com/nmap/nmap.git synced 2026-01-01 12:29:03 +00:00

Adds case insensitive pattern generator function. Closes

#853
This commit is contained in:
rewanth
2017-06-08 08:20:19 +00:00
parent 3d9f22e2f9
commit ab1eb4c037

View File

@@ -1445,4 +1445,29 @@ function keys(t)
return ret
end
-- Returns the case insensitive pattern of given parameter
-- Useful while doing case insensitive pattern match using string library.
-- https://stackoverflow.com/questions/11401890/case-insensitive-lua-pattern-matching/11402486#11402486
--
-- Ex: generate_case_insensitive_pattern("user") = "[uU][sS][eE][rR]"
--
-- @param pattern The string
-- @return A case insensitive patterned string
function generate_case_insensitive_pattern(pattern)
-- Find an optional '%' (group 1) followed by any character (group 2)
local p = pattern:gsub("(%%?)(.)", function(percent, letter)
if percent ~= "" or not letter:match("%a") then
-- If the '%' matched, or `letter` is not a letter, return "as is"
return percent .. letter
else
-- Else, return a case-insensitive character class of the matched letter
return format("[%s%s]", letter:lower(), letter:upper())
end
end)
return p
end
return _ENV;