Lua sample guide

I am trying to implement a template in Lua, but will fail

I need a pattern that looks like a regular expression: [a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}

which should check guid .

I cannot find the right way to find a regex for implementation in Lua and could not find in the documentation.

Please help me implement the above regex for guid.

+4
source share
1 answer

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:

  • The modifier is {8}not supported in the Lua template.
  • -must be done with %-.
  • The character class is %xequivalent [0-9a-fA-F].

A clear way to build a template using the helper table provided by @ 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, '%-')
+8
source

Source: https://habr.com/ru/post/1536109/


All Articles