示例工程 10-pattern-matching

10-pattern-matching/04-greedy-vs-lazy/main.lua

lua · 0.4 KB

-- @runtimes lua54 luajit
--- 演示:贪婪 * 与非贪婪 - 的差异。
local M = {}
 
--- @return table { greedy = string, lazy = string }
function M.run(log)
	log = log or print
	local text = "<a><b>"
	local greedy = text:match("<(.*)>")
	local lazy = text:match("<(.-)>")
	log(("greedy=%s"):format(greedy))
	log(("lazy=%s"):format(lazy))
	return { greedy = greedy, lazy = lazy }
end
 
if not ... then
	M.run()
end
return M