不区分大小写的 Lua 模式匹配 [英] Case-insensitive Lua pattern-matching
问题描述
我正在为运行 Windows CE 6/7 的移动设备编写 Lua 中的 grep 实用程序,但在实现不区分大小写的匹配模式时遇到了一些问题.由于字符类的原因,将所有内容转换为大写(或小写)的明显解决方案并不那么简单.
I'm writing a grep utility in Lua for our mobile devices running Windows CE 6/7, but I've run into some issues implementing case-insensitive match patterns. The obvious solution of converting everything to uppercase (or lower) does not work so simply due to the character classes.
我唯一能想到的就是将模式本身中的文字转换为大写.
The only other thing I can think of is converting the literals in the pattern itself to uppercase.
这是我目前所拥有的:
function toUpperPattern(instr)
-- Check first character
if string.find(instr, "^%l") then
instr = string.upper(string.sub(instr, 1, 1)) .. string.sub(instr, 2)
end
-- Check the rest of the pattern
while 1 do
local a, b, str = string.find(instr, "[^%%](%l+)")
if not a then break end
if str then
instr = string.sub(instr, 1, a) .. string.upper(string.sub(instr, a+1, b)) .. string.sub(instr, b + 1)
end
end
return instr
end
我不愿意承认达到那么远需要多长时间,而且我仍然可以立即看到存在诸如转义百分号 '%%' 之类的问题
I hate to admit how long it took to get even that far, and I can still see right away there are going to be problems with things like escaped percent signs '%%'
我认为这一定是一个相当普遍的问题,但我似乎找不到关于该主题的太多内容.有没有更简单(或至少完整)的方法来做到这一点?我在这里开始发疯了......希望各位Lua高手能赐教!
I figured this must be a fairly common issue, but I can't seem to find much on the topic. Are there any easier (or at least complete) ways to do this? I'm starting to go crazy here... Hoping you Lua gurus out there can enlighten me!
推荐答案
试试这个:
function case_insensitive_pattern(pattern)
-- find an optional '%' (group 1) followed by any character (group 2)
local p = pattern:gsub("(%%?)(.)", function(percent, letter)
if percent ~= "" or not letter:match("%a") then
-- if the '%' matched, or `letter` is not a letter, return "as is"
return percent .. letter
else
-- else, return a case-insensitive character class of the matched letter
return string.format("[%s%s]", letter:lower(), letter:upper())
end
end)
return p
end
print(case_insensitive_pattern("xyz = %d+ or %% end"))
打印:
[xX][yY][zZ] = %d+ [oO][rR] %% [eE][nN][dD]
这篇关于不区分大小写的 Lua 模式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!