[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: 回复: help with string:match
- From: 张睿 <zrui16@...>
- Date: Sun, 1 Mar 2015 22:28:19 +0800
Something like (not tested, and you need a split() implementation):
t1 = split(s1, '/') --> {'', 'foo', ':bar', 'oof', ':rab'}
t2 = split(s2, '/') --> {'', 'foo', 'lua', 'oof', 'rocks'}
r = {}
if #t1 ~= #t2 then
return false, "not match"
end
for i = 1, #t1 do
if t1[i]:sub(1, 1) == ':' then
r[t1[i]:sub(2)] = t2[i]
elseif t1[i] ~= t2[i] then
return false, "not match"
end
end
return r
-------- 原始邮件 --------
日期:2015-03-01 21:04 (GMT+08:00)
收件人: Lua mailing list
主题: help with string:match
Hello everyone,
my knowledge about regular expressions is very limited, so I'm struggling with this problem:
Given 2 strings
s1 = '/foo/:bar/oof/:rab'
s2 = '/foo/lua/oof/rocks'
I would like to produce the following information
1) If they match (these two above should match)
2) a table holding the values of 's2' in with the corresponding name in 's1'
In this case we would have: { bar = "lua", rab = "rocks" }
I think this algorithm solves it, but I cant figure how to implement it:
a) store the placeholders ':' indexes as KEYS of a table, and the respective VALUES being the name of these placeholders
example with s1:
local aux1 = { "6" = "bar", "15" = "rab" }
b) with the keys of 'aux1' fetched as indexes, extract the values of 's2'
into another table
local aux2 = {"6" = "lua", "15" = "rocks}
c) finally merge them two into one table (this one is easy :P)
{ bar = "lua", rab = "rocks" }
Thank you!