【问题标题】:Regex works everywhere but in Lua正则表达式在任何地方都有效,但在 Lua 中
【发布时间】:2017-10-12 00:34:03
【问题描述】:

我的正则表达式在 Lua 中不起作用。我已经在我的文本编辑器和一些在线正则表达式工具等其他环境中对其进行了测试,它似乎在那里工作正常。

在其他环境中按我想要的方式工作的正则表达式:

RAY\.decrypt\s*\(([\"\'].+?(?=[\"\']).+?(?=[\)]))\s*\)

我试图在 Lua 中使用的正则表达式(只是 \'s 替换为 %'s)

RAY%.decrypt%s*%(([\"\'].+?(?=[\"\']).+?(?=[%)]))%s*%)

我尝试匹配的示例文本(我想捕获括号中的内容)

RAY.decrypt ("\x02\x02\x02\x02\x02\x02\x02\x02")
RAY.decrypt ("\xd6E\xd6E\xd6E\xd6E\xd6E")
RAY.decrypt("\x8e\x8e\x8e\x8e\x8e\x8e\x8e\x8e")

其他工具可以完美匹配文本并捕捉我想要捕捉的内容。但我正在努力让 Lua 做到这一点,因为它不匹配任何东西。

local s = [[RAY.decrypt ("\x02\x02\x02\x02\x02\x02\x02\x02")
RAY.decrypt ("\xd6E\xd6E\xd6E\xd6E\xd6E")
RAY.decrypt("\x8e\x8e\x8e\x8e\x8e\x8e\x8e\x8e")]]

print(string.find(s, "RAY%.decrypt%s*%(([\"\'].+?(?=[\"\']).+?(?=[%)]))%s*%)"))
> nil

任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: regex lua


    【解决方案1】:
    local s = [[
    RAY.decrypt ("\x02\x02\x02\x02\x02\x02\x02\x02")
    RAY.decrypt ('\xd6E\xd6E\xd6E\xd6E\xd6E')
    RAY.decrypt( "\x8e\x8e\x8e\x8e\x8e\x8e\x8e\x8e" )
    ]]
    
    for w in s:gmatch"RAY%.decrypt%s*%(%s*(([\"']).-%2)%s*%)" do
       print(w)
    end
    

    输出:

    "\x02\x02\x02\x02\x02\x02\x02\x02"
    '\xd6E\xd6E\xd6E\xd6E\xd6E'
    "\x8e\x8e\x8e\x8e\x8e\x8e\x8e\x8e"
    

    【讨论】:

      【解决方案2】:

      我根本不了解 LUA,但 wikipedia 提到了 LUA 正则表达式引擎: “使用简化的、有限的方言;可以绑定到更强大的库,例如 PCRE 或替代解析器,例如 LPeg。”

      因此,您应该将其绑定到 PCRE,或者您不应该与其他引擎进行比较,而只是使用 LUA 文档专门为 LUA 编写正则表达式,而不是您的文本编辑器。

      【讨论】:

      • 非常感谢。 Lua 正则表达式引擎似乎不支持前瞻,这解释了它为什么不起作用。
      【解决方案3】:

      Lua 的标准字符串库不支持完整的正则表达式,但它的模式匹配非常强大。

      此脚本匹配括号内的所有内容:

      for w in s:gmatch("%((.-)%)") do
         print(w)
      end
      

      【讨论】:

      • 如果文字字符串包含右括号怎么办?原始正则表达式负责:RAY\.decrypt\s*\(([\"\'].+?(?=[\"\']).+?(?=[\)]))\s*\)
      【解决方案4】:

      Lua 内置的“正则表达式”称为“模式”

      这匹配括号中的内容

      local source = [[
        RAY.decrypt ("\x02\x02\x02\x02\x02\x02\x02\x02")
        RAY.decrypt ("\xd6E\xd6E\xd6E\xd6E\xd6E")
        RAY.decrypt("\x8e\x8e\x8e\x8e\x8e\x8e\x8e\x8e")
      ]]
      
      -- this captures expressions containing RAY.decrypt ( " ... " )
      for content in string.gmatch(source , "RAY%.decrypt%s*%(%s*\"(.-)\"%s*%)") do
         print(content)
      end
      
      -- this captures expressions inside double quotes
      for content in string.gmatch(source , "\"(.-)\"") do
         print(content)
      end
      
      -- this captures expressions inside single or double quotes e.g. RAY.decrypt  ( '\x8e\x8e\x8e\x8e\x8e\x8e\x8e\x8e')
      for content in string.gmatch(source , "[\"'](.-)[\"']") do
         print(content)
      end
      
      -- this captures expressions inside parentheses (will capture the quotes)
      for content in string.gmatch(source , "%((.-)%)") do
         print(content)
      end
      

      【讨论】:

        猜你喜欢
        • 2014-05-19
        • 1970-01-01
        • 1970-01-01
        • 2021-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-06
        相关资源
        最近更新 更多