请查看此问题Pattern ^u.meta(\.|$) not working as expected,该问题具有我需要的预期行为。
更改
在模式ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, configureList(idRuta, rocodromo, dificultad))
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
if(colores.get(position).equals("0"))
{
text.setBackgroundColor(Color.GREEN);
}
else if(colores.get(position).equals("1"))
{
text.setBackgroundColor(Color.RED);
}
else if(colores.get(position).equals("2"))
{
text.setBackgroundColor(Color.YELLOW);
}
else
{
text.setBackgroundColor(Color.WHITE);
}
return view;
}
};
或lua ^u.meta(\.|$)
或'^u%.meta%f[\0.]'
中,我需要的更改'^u%.meta%f[%z.]'
可以是用户从变量定义的任何内容。模式应该是通用的/动态的,以匹配变量中设置的内容。
例如:
u.meta
解决方案1
所以,如果可能的话,我在想的是lua模式中的-- should return 'u.meta', and pattern should match
local pattern = 'u.meta'
print(string.match("u.meta.admin", '^u%.meta%f[\0.]')) -- u.meta
-- should return 'nil', and pattern should fail
local pattern = 'u.meta'
print(string.match("u.domain.admin", '^u%.meta%f[\0.]')) -- nil
-- should return 'anything.anything', and pattern should match
local pattern = 'anything.anything'
print(string.match("anything.anything.something", '^anything%.anything%f[\0.]') -- anything.anything
-- should return nil, and pattern should fail
local pattern = 'anything.anything'
print(string.match("fake.fake.something", '^anything%.anything%f[\0.]') -- nil
。
interpolation
工作解决方案2
我已经在方法的帮助下工作了。但我仍然需要手动调用这些模式。如果我们可以从模式本身解决这个问题,那就太棒了
示例:
"^#{pattern}%f[\0.]"
答案 0 :(得分:1)
如果您需要支持用户输入作为正则表达式模式的文字部分,则需要引入一个转义函数,以便使用%
转义所有魔术字符。然后,只与自定义边界(^
字符串的开头连接,并且
%f[%z.]
表示字符串或点的结尾。
function escape (s)
return string.gsub(s, '[.*+?^$()[%%-]', "%%%0")
end
function pattern_matcher(v, pattern) return string.match(v, pattern) end
word = "u.meta"
print(pattern_matcher("u.meta.something", '^' .. escape(word) .. '%f[%z.]')) -- u.meta
请参阅this demo
在escape
函数中,替换模式中的前两个%%
表示一个%
,%0
反向引用整个匹配({{3}之一})
答案 1 :(得分:1)
这是我的最终解决方案:
-- USAGE:
--
-- local t = {}
-- t['roles'] = 'u.meta.admin'
-- match_roles(t, 'u.meta') -- u.meta
-- match_roles(t, 'u.fake') -- nil
--
-- SOLUTION 1
--------------
--
-- function u_meta(v) return string.match(v, '^u%.meta%f[%z.]') end
-- function u_domain(v) return string.match(v, '^u%.domain%f[%z.]') end
--
-- function match_roles(table, pattern)
-- for _, value in pairs(table) do
-- if pattern == "u.meta" then
-- if pattern == u_meta(value) then return true end
-- elseif pattern == "u.domain" then
-- if pattern == u_domain(value) then return true end
-- end
-- return false
-- end
-- end
-- SOLUTION 2
---------------
--
-- function pattern_matcher(v, p) return string.match(v, p) end
-- function match_roles(table, role, pattern)
-- for _, value in pairs(table) do
-- if role == pattern_matcher(value, pattern) then return true end
-- return false
-- end
-- end
-- SOLUTIN - 3 (GENERIC SOLUTION) - Thanks Wiktor for pattern
---------------------------------
function escape (s) return string.gsub(s, '[.*+?^$()[%%-]', "%%%0") end
function pattern_matcher(v, pattern) return string.match(v, pattern) end
function match_roles(table, pattern)
for _, value in pairs(table) do
if pattern == pattern_matcher(value, '^' .. escape(pattern) .. '%f[%z.]') then return true end
return false
end
end
-- UNIT TEST
-- ---------
--
-- Below section covers unit test in lua.
-- we are using `luaunit` unit-testing framework that works for lua.
-- NOTE: LuaUnit works with Lua 5.1, 5.2, 5.3 and luajit (v1 and v2.1),
-- http://luaunit.readthedocs.org/en/latest/
--
package.path = './lib/?.lua;' .. package.path
luaunit = require('luaunit')
local t = {}
function test_meta_user_should_be_true()
t["roles"] = 'u.meta.admin.system'
luaunit.assertEquals( match_roles(t, 'u.meta'), true )
end
function test_meta_admin_should_be_true()
t["roles"] = 'u.meta.admin'
luaunit.assertEquals( match_roles(t, 'u.meta'), true )
end
function test_system_admin_should_be_true()
t["roles"] = 'u.meta.admin.system'
luaunit.assertEquals( match_roles(t, 'u.meta'), true )
end
function test_invalid_meta_admin_should_be_false()
t["roles"] = 'u.meta_admin'
luaunit.assertEquals( match_roles(t, 'u.meta'), false )
end
function test_invalid_meta_admin_system_should_be_false()
t["roles"] = 'u.meta_admin_system'
luaunit.assertEquals( match_roles(t, 'u.meta'), false )
end
function test_invalid_role_should_be_false()
t["roles"] = 'u.meta-admin'
luaunit.assertEquals( match_roles(t, 'u.meta'), false )
end
function test_domain_should_not_allow_in_meta()
t['roles'] = 'u.domain'
luaunit.assertEquals( match_roles(t, 'u.meta'), false )
end
function test_domain_user_should_be_true()
t["roles"] = 'u.domain'
luaunit.assertEquals( match_roles(t, 'u.domain'), true )
end
function test_domain_admin_should_be_true()
t["roles"] = 'u.domain.admin'
luaunit.assertEquals( match_roles(t, 'u.domain'), true )
end
function test_fake_domain_admin_should_be_falsy()
t["roles"] = 'u.domain_admin'
luaunit.assertEquals( match_roles(t, 'u.domain'), false )
end
function test_fake_role_should_be_falsy()
t["roles"] = 'u.domain-admin'
luaunit.assertEquals( match_roles(t, 'u.domain'), false )
end
function test_user_should_either_domain_or_meta ()
t["roles"] = 'u'
luaunit.assertEquals( match_roles(t, 'u.domain'), false )
end
function test_meta_user_in_domain_should_be_false ()
t["roles"] = 'u.meta'
luaunit.assertEquals( match_roles(t, 'u.domain'), false )
end
function test_meta_admin_in_domain_should_be_false ()
t["roles"] = 'u.meta.admin'
luaunit.assertEquals( match_roles(t, 'u.domain'), false )
end
function test_system_admin_in_domain_should_be_false ()
t["roles"] = 'u.meta.admin.system'
luaunit.assertEquals( match_roles(t, 'u.domain'), false )
end
function test_fake_meta_admin_in_domain_should_be_true ()
t["roles"] = 'u.meta_admin'
luaunit.assertEquals( match_roles(t, 'u.domain'), false )
end
function test_fake_system_admin_in_domain_should_be_true ()
t["roles"] = 'u.meta_admin_system'
luaunit.assertEquals( match_roles(t, 'u.domain'), false )
end
function test_fake_meta_role_in_domain_should_be_true ()
t["roles"] = 'u.meta-admin'
luaunit.assertEquals( match_roles(t, 'u.domain'), false )
end
function test_anything_should_match_correct_pattern()
t['roles'] = 'a.b.c.z'
luaunit.assertEquals( match_roles(t, 'a.b'), true )
end
function test_anything_should_fail_incorrect_pattern()
t['roles'] = 'a.b_c'
luaunit.assertEquals( match_roles(t, 'a.b'), false )
end
-- Exit after testcases finished
os.exit( luaunit.LuaUnit.run() )