不区分大小写的Lua模式匹配 [英] Case-insensitive Lua pattern-matching

查看:1019
本文介绍了不区分大小写的Lua模式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Lua中为运行Windows CE 6/7的移动设备编写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.

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

Here's what I have so far:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆