blob: fc4067247119816955adedb27d7636b0895d1c1a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#!/opt/local/bin/lua
--
-- See ../README.md
--
--
-- Run as: lua ch-1.lua < input-file
--
for line in io . lines () do
--
-- Extract the string and pattern
--
local str, pattern = line : match ("(%S+) +(%S+)")
--
-- Turn the pattern into a Lua pattern, replace any
-- character c:
-- if c == "?" -> .
-- if c == "*" -> .*
-- if c is not an alpanum -> %c
-- otherwise, keep c as is
-- And then add anchors
--
pattern = pattern : gsub (".", function (c)
local result = c
if c == "?" then
result = "."
elseif c == "*" then
result = ".*"
elseif c : match ("%W") then
result = "%" .. c
end
return result
end)
pattern = "^" .. pattern .. "$"
--
-- Print 1/0 depending whether we have a match or not.
--
if (str : match (pattern)) then
print (1)
else
print (0)
end
end
|