MATCH AND CATCHSuppose we want to replace in a text all instances of the
misspelling seperate by separate. Here is a script to do it:
#!luaThe first argument of gsub is the pattern to find and the second is the text to replace it.Suppose you want to print out in alphabetical order all the kinds of tag that occur in marked-up text. Here is a script for that:
for linevariable: you choose its name in ioinput/output library.table memberlines ( arg[1]the textfile ) do
line = line:gsubglobally substitute ("seperat", "separat")
print (line)
end -- for
#!luaSuppose we want to add up all the numbers in a text that are preceded by a £-sign. We can do this using a function as the second argument of gsub as follows:
local patvariable: you choose its name = "<(%a+)"tagname
local tag, usedvariable: you choose its name = { }, { }
local insert, sort in table
local capturevariable: you choose its name = \ (x)
if not used[x] then
insert ( tag, x)
used[x] = true
end -- if
end -- function
for linevariable: you choose its name in ioinput/output library.table memberlines ( arg[1]the textfile ) do
line:gsub ( pat, capture)
end -- for
sort (tag)
for _, x in ipairs (tag) do
print (x)
end -- for
#!luaThe pattern dosh is the pattern we are searching for. Its single pair of parentheses carve out what we want to capture. The function gsub passes this on to addup which converts it to a number and adds it to total.By contrast, we can also use the lpeg library whose pattern and matching facilities are rather different from those of the string library. This script does the same job as the one one above.
local totalvariable: you choose its name = 0create a number
local doshvariable: you choose its name = "£(%-?%d+%.?%d+)"number
local addupvariable: you choose its name = \ (x) totalvariable: you choose its name + = tonumber (x) end
for linevariable: you choose its name in ioinput/output library.table memberlines ( arg[1]the textfile ) do
line:gsub (dosh, addup)
end -- for
print (total)
#!luaIn this example we have met:
local number, digits in bc
digits (6)
local R, P, C, Cf, Cc in lpeg
local numvariable: you choose its name ---- uses R, P, C
do
local digit, minus, point variable: you choose its name = R "09", P "-", P "."
local fracvariable: you choose its name = pointvariable: you choose its name * followed bydigitvariable: you choose its name^1
numvariable: you choose its name = "£" * followed byC(minusvariable: you choose its name^(-1) * followed bydigitvariable: you choose its name^1 * followed byfracvariable: you choose its name^(-1))/number
end-- do
local numbersvariable: you choose its name ---- uses Cf, Cc, num
do
local sumvariable: you choose its name = \ (x, y) => x + y end
numbersvariable: you choose its name = Cf (Cc (0) * followed by(numvariable: you choose its name + 1skip one character if num fails)^0, and repeatsumvariable: you choose its name)
end-- do
local totalvariable: you choose its name = 0
for linevariable: you choose its name in ioinput/output library.table memberlines ( arg[1]the textfile ) do
totalvariable: you choose its name + = numbersvariable: you choose its name:match (linevariable: you choose its name)
end -- for
print (total)
global substitution, replacement functions, patterns, captures, table.sort, ipairs iterator, lpeg