lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Sorry if my previous post used another approach and did not look into your algorithm. I think there's a problem with your algorithm: the index you get in s1 is not valid for s2; since ":foo" and "lua" don't share the same length, index for ":rab" in s1 and "rocks" in s2 will be different.

-------- 原始邮件 --------
发件人: Pablo Botelho
日期: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!