Testing a string for a match

Regular expressions

-- Lua patterns, not regular expressions
local data1 = "aaab"
local data2 = "aaaba"
local data3 = "bbba"

-- ^ pins the match to the start,
-- the end of the string is free
local pattern = "^a+b"

local b1 = data1:find(pattern) ~= nil
-- b1 = true
local b2 = data2:find(pattern) ~= nil
-- b2 = true
local b3 = data3:find(pattern) ~= nil
-- b3 = false

-- The same check, but with the matched text
local m2 = data2:match(pattern)
-- m2 = aaab
local m3 = data3:match(pattern)
-- m3 = nil

-- Without ^ the match may start anywhere
local data4 = "baab"
local anywhere = data4:find("a+b") ~= nil
-- anywhere = true

print("b1 = " .. tostring(b1))
print("b2 = " .. tostring(b2))
print("b3 = " .. tostring(b3))
print("m2 = " .. m2)
print("m3 = " .. tostring(m3))
print("anywhere = " .. tostring(anywhere))