Gua的Lua模式

use*_*906 4 regex lua guid lua-patterns

我试图在Lua中实现一个模式,但没有成功

我需要的模式就像正则表达式: [a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}

这是验证guid.

我无法找到在Lua中找到工具正则表达式的正确方法,也无法在文档中找到.

请帮我实现guid以上的正则表达式.

Yu *_*Hao 8

你可以用这个:

local pattern = "%x%x%x%x%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%x%x%x%x%x%x%x%x"
local guid = "3F2504E0-4F89-41D3-9A0C-0305E82C3301"
print(guid:match(pattern))
Run Code Online (Sandbox Code Playgroud)

注意:

  1. {8}Lua模式不支持修饰符.
  2. -需要逃脱%-.
  3. 字符类%x相当于[0-9a-fA-F].

使用@ hjpotter92提供的辅助表构建模式的明确方法:

local x = "%x"
local t = { x:rep(8), x:rep(4), x:rep(4), x:rep(4), x:rep(12) }
local pattern = table.concat(t, '%-')
Run Code Online (Sandbox Code Playgroud)