不区分大小写的Lua模式匹配。

我正在为运行 Windows CE 6/7 的移动设备编写 Lua 中的 grep 实用程序,但是我在实现不区分大小写的匹配模式时遇到了一些问题。显然的解决方案是将所有内容转换为大写(或小写),但由于字符类,这并不那么简单。

我能想到的唯一解决方法是将模式中的文字本身转换为大写。

到目前为止,这是我所拥有的内容:

function toUpperPattern(instr)
    -- 检查第一个字符
    if string.find(instr, "^%l") then
        instr = string.upper(string.sub(instr, 1, 1)) .. string.sub(instr, 2)
    end
    -- 检查模式的其余部分
    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

我不愿承认实现这一步骤所用的时间有多长,而且我仍然可以立即发现在处理转义百分号 '%%' 等问题时会出现问题。

我想这必须是一个相当常见的问题,但是我似乎找不到关于这个主题的太多内容。 有更简单(或至少完整)的方法吗? 我开始疯了...... 希望你们Lua大师能启发我!

点赞
用户50476
用户50476
function case_insensitive_pattern(pattern)

  -- 查找可选的 '%'(第一个组)后跟任何字符(第二个组)
  local p = pattern:gsub("(%%?)(.)", function(percent, letter)

    if percent ~= "" or not letter:match("%a") then
      -- 如果 '%' 匹配或 'letter' 不是字母,返回“原样”
      return percent .. letter
    else
      -- 否则,返回大小写不敏感的字符类
      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]
2012-07-09 20:19:47
用户6544989
用户6544989
do
    local p = re.compile([[
        pattern  <- ( {b} / {escaped} / brackets / other)+
        b        <- "%b" . .
        escaped  <- "%" .
        brackets <- { "[" ([^]%]+ / escaped)* "]" }
        other    <- [^[%]+ -> cases
    ]], {
        cases = function(str) return (str:gsub('%a',function(a) return '['..a:lower()..a:upper()..']' end)) end
    })
    local pb = re.compile([[
        pattern  <- ( {b} / {escaped} / brackets / other)+
        b        <- "%b" . .
        escaped  <- "%" .
        brackets <- {: {"["} ({escaped} / bcases)* {"]"} :}
        bcases   <- [^]%]+ -> bcases
        other    <- [^[%]+ -> cases
    ]], {
        cases = function(str) return (str:gsub('%a',function(a) return '['..a:lower()..a:upper()..']' end)) end
        , bcases = function(str) return (str:gsub('%a',function(a) return a:lower()..a:upper() end)) end
    })
    function iPattern(pattern,brackets)
        ('sanity check'):find(pattern)
        return table.concat({re.match(pattern, brackets and pb or p)})
    end
end

local test                  = '[ab%c%]d%%]+ o%%r %bnm'
print(iPattern(test))       -- [ab%c%]d%%]+ [oO]%%[rR] %bnm
print(iPattern(test,true))  -- [aAbB%c%]dD%%]+ [oO]%%[rR] %bnm
print(('qwe [%D]% O%r n---m asd'):match(iPattern(test, true))) -- %D]% O%r n---m

纯 Lua 版本:

由于 Lua 模式不像正则表达式(abc|something)那样具有选择,所以必须分析字符串中的所有字符以将其转换为正确的模式。

function iPattern(pattern, brackets)
    ('sanity check'):find(pattern)
    local tmp = {}
    local i=1
    while i <= #pattern do              -- 'for' don't let change counter
        local char = pattern:sub(i,i)   -- 当前字符
        if char == '%' then
            tmp[#tmp+1] = char          -- 添加到 tmp 表中
            i=i+1                       -- 下一个字符位置
            char = pattern:sub(i,i)
            tmp[#tmp+1] = char
            if char == 'b' then         -- '%bxy' - 添加下两个字符
                tmp[#tmp+1] = pattern:sub(i+1,i+2)
                i=i+2
            end
        elseif char=='[' then           -- 括号
            tmp[#tmp+1] = char
            i = i+1
            while i <= #pattern do
                char = pattern:sub(i,i)
                if char == '%' then     -- 没有 '%bxy' 在括号里
                    tmp[#tmp+1] = char
                    tmp[#tmp+1] = pattern:sub(i+1,i+1)
                    i = i+1
                elseif char:match("%a") then    -- 字母
                    tmp[#tmp+1] = not brackets and char or char:lower()..char:upper()
                else                            -- 其他的
                    tmp[#tmp+1] = char
                end
                if char==']' then break end -- 关闭括号
                i = i+1
            end
        elseif char:match("%a") then    -- 字母
            tmp[#tmp+1] = '['..char:lower()..char:upper()..']'
        else
            tmp[#tmp+1] = char          -- 其他的
        end
        i=i+1
    end
    return table.concat(tmp)
end

local test                  = '[ab%c%]d%%]+ o%%r %bnm'
print(iPattern(test))       -- [ab%c%]d%%]+ [oO]%%[rR] %bnm
print(iPattern(test,true))  -- [aAbB%c%]dD%%]+ [oO]%%[rR] %bnm
print(('qwe [%D]% O%r n---m asd'):match(iPattern(test, true))) -- %D]% O%r n---m
2016-07-04 06:55:08