dimanche 24 novembre 2019

Find padded hex (0xXX) in Lua string pattern

I have a Lua string...

str = "test,0x00,0x34,0x23"

and I want to find two elements within this string:

  1. The alphanumeric part (test)
  2. The comma separated hex values (0xXX)

I'm using the following pattern, which works...

_,_,a,b = string.find(str,"^(%a+),([x%x,]+)$")
print(a,b)

-->    test    0x00,0x34,0x23

However, I want to catch errors, for example, malformed hex representations.

For example, using the above pattern example, it allows and successfully returns the following inputs:

str = "test,0x0x0,0x34,0x23"   -->    test    0x0x0,0x34,0x23
str = "test,0x00,34,0x23"      -->    test    0x00,34,0x23
str = "test,0x00,0x34,23x0"    -->    test    0x00,0x34,23x0

What pattern is needed to only accept valid padded hex form (string.format("0x%02x",72)) please?

Aucun commentaire:

Enregistrer un commentaire