简体   繁体   English

Gua的Lua模式

[英]Lua pattern for guid

I am trying to implement a pattern in Lua but no success 我试图在Lua中实现一个模式,但没有成功

The pattern I need is like regex: [a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12} 我需要的模式就像正则表达式: [a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}

which is to validate guid . 这是验证guid

I am not able to find proper way to find the implement regex in Lua and not able to find in documentation also. 我无法找到在Lua中找到工具正则表达式的正确方法,也无法在文档中找到。

Please help me to implement above regex for guid. 请帮我实现guid以上的正则表达式。

You can use this: 你可以用这个:

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))

Note that: 注意:

  1. Modifier {8} is not supported in Lua pattern. Lua模式不支持修饰符{8}
  2. - needs to be escaped with %- . -需要使用%-进行转义。
  3. Character class %x is equivalent to [0-9a-fA-F] . 字符类%x相当于[0-9a-fA-F]

A clear way to build the pattern using an auxiliary table, provided by @hjpotter92: 使用@ 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, '%-')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM