-- Lua patterns, not regular expressions
local data = "Pi is equal to 3.14"
-- %d is a digit, %. is a dot, + is one or more
local pattern = "%d+%.%d+"
-- match returns the matched text, or nil
local found = data:match(pattern)
-- found = 3.14
local pi
if found then
pi = tonumber(found)
end
-- pi = 3.14
-- find says where the match is
local first, last = data:find(pattern)
-- first = 16
-- last = 19
-- Parentheses capture parts of the match
local int_part, frac_part =
data:match("(%d+)%.(%d+)")
-- int_part = 3
-- frac_part = 14
-- No match: nil, not an error
local missing = data:match("%d+x")
-- missing = nil
print("found = " .. found)
print("pi = " .. pi)
print("first = " .. first)
print("last = " .. last)
print("int_part = " .. int_part)
print("frac_part = " .. frac_part)
print("missing = " .. tostring(missing))