【问题标题】:Match repeatable string as a "whole word" in Lua 5.1在 Lua 5.1 中将可重复字符串匹配为“整个单词”
【发布时间】:2019-02-04 04:08:51
【问题描述】:

我的环境:

  • Lua 5.1
  • 绝对不能使用带有本机组件(如 C .so/.dll)的库
  • 我可以运行任意纯 Lua 5.1 代码,但我无法访问 os 和其他几个允许访问本机文件系统、shell 命令或类似内容的包,因此所有功能都必须在 Lua 中实现本身(仅)。
  • 我已经成功加入LuLpeg。我可能可以引入其他纯 Lua 库。

我需要编写一个函数,如果输入字符串匹配任意字母和数字序列作为重复一次或多次的整个单词,并且可能在整个匹配子串的开始或结束。我使用“整个单词”的含义与 PCRE 单词边界 \b 相同。

为了演示这个想法,这里是使用 LuLpeg 的 re 模块的错误尝试;它似乎适用于负前瞻,但不适用于负前瞻背后

function containsRepeatingWholeWord(input, word)
    return re.match(input:gsub('[%a%p]+', ' %1 '), '%s*[^%s]^0{"' .. word .. '"}+[^%s]^0%s*') ~= nil
end

这里是示例字符串和预期的返回值(引号是语法上的,就像输入到 Lua 解释器中一样,而不是字符串的文字部分;这样做是为了使尾随/前导空格明显):

  • 输入: " one !tvtvtv! two"word: tv返回值: true
  • 输入: "I'd"word: d返回值: false
  • 输入: "tv"word: tv返回值: true
  • 输入: " tvtv! "word: tv返回值: true
  • 输入: " epon "word: nope返回值: false
  • 输入: " eponnope "word: nope返回值: false
  • 输入: "atv"word: tv返回值: false

如果我有一个完整的 PCRE 正则表达式库,我可以很快做到这一点,但我没有,因为我无法链接到 C,而且我还没有找到 PCRE 或类似的任何纯 Lua 实现。

我不确定 LPEG 是否足够灵活(直接使用 LPEG 或通过其 re 模块)来做我想做的事,但我很确定 Lua 的内置函数不能做我想做的事,因为它无法处理重复的字符序列。 (tv)+ 不适用于 Lua 的内置 string:match 函数和类似函数。

我一直在寻找有趣的资源,试图弄清楚如何做到这一点,但无济于事:

【问题讨论】:

    标签: regex lua lua-5.1 lpeg


    【解决方案1】:

    我认为该模式不能可靠地工作,因为%s*[^%s]^0 部分匹配一系列可选的空格字符,后跟非空格字符,然后它尝试匹配重复的单词并失败。之后,它不会在字符串中向后或向前移动并尝试在另一个位置匹配重复的单词。 LPeg 和re 的语义与大多数正则表达式引擎的语义非常不同,即使对于看起来相似的东西也是如此。

    这是基于re 的版本。该模式有一个捕获(重复的单​​词),因此如果找到重复的单词,匹配返回一个字符串而不是一个数字。

    function f(str, word)
        local patt = re.compile([[
            match_global <- repeated / ( [%s%p] repeated / . )+
            repeated <- { %word+ } (&[%s%p] / !.) ]],
            { word = word })
        return type(patt:match(str)) == 'string'
    end
    

    这有点复杂,因为普通的re 没有办法生成lpeg.B 模式。

    这是使用lpeg.Blpeg 版本。 LuLPeg 也适用于此。

    local lpeg = require 'lpeg'
    lpeg.locale(lpeg)
    
    local function is_at_beginning(_, pos)
        return pos == 1
    end
    
    function find_reduplicated_word(str, word)
        local type, _ENV = type, math
        local B, C, Cmt, P, V = lpeg.B, lpeg.C, lpeg.Cmt, lpeg.P, lpeg.V
        local non_word = lpeg.space + lpeg.punct
        local patt = P {
            (V 'repeated' + 1)^1,
            repeated = (B(non_word) + Cmt(true, is_at_beginning))
                    * C(P(word)^1)
                    * #(non_word + P(-1))
        }
        return type(patt:match(str)) == 'string'
    end
    
    for _, test in ipairs {
        { 'tvtv', true },
        { ' tvtv', true },
        { ' !tv', true },
        { 'atv', false },
        { 'tva', false },
        { 'gun tv', true },
        { '!tv', true },
    } do
        local str, expected = table.unpack(test)
        local result = find_reduplicated_word(str, 'tv')
        if result ~= expected then
            print(result)
            print(('"%s" should%s match but did%s')
                :format(str, expected and "" or "n't", expected and "n't" or ""))
        end
    end
    

    【讨论】:

    • 这是 lpeg.relpeg 可以做什么的优秀文档,但是因为在我的例子中 LPEG 是解释 Lua 代码,string 是作为 Lua 运行时一部分的本机 C 代码,Egor 的解决方案更快。我想,如果内置的 string 函数不能满足我的需要,我只会在我的环境中使用 LuLPeg。但我投了赞成票,这无疑帮助我更好地了解 LPeg 以满足未来的需求。
    • 是的,我想string 基于库的解决方案会比 LuLPeg 更快,内存效率更高。
    【解决方案2】:

    Lua 模式已经足够强大了。
    这里不需要 LPEG。

    这是你的功能

    function f(input, word)
       return (" "..input:gsub(word:gsub("%%", "%%%%"), "\0").." "):find"%s%p*%z+%p*%s" ~= nil
    end
    

    这是功能测试

    for _, t in ipairs{
       {input = " one !tvtvtv! two", word = "tv", return_value = true},
       {input = "I'd", word = "d", return_value = false},
       {input = "tv", word = "tv", return_value = true},
       {input = "   tvtv!  ", word = "tv", return_value = true},
       {input = " epon ", word = "nope", return_value = false},
       {input = " eponnope ", word = "nope", return_value = false},
       {input = "atv", word = "tv", return_value = false},
    } do
       assert(f(t.input, t.word) == t.return_value)
    end
    

    【讨论】:

    • 这教会了我很多关于 Lua 的内置正则表达式可以做什么。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    相关资源
    最近更新 更多