lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Dirk Laurie once stated:
> 2014-10-27 17:54 GMT+02:00 Jain, Punit <Punit.Jain@emc.com>:
> 
> > How can I check whether a string is a hexadecimal number or not? E.g.
> > “0x123” is a hex string, but “123” and “xyz” are not.
> 
> str:match("0x[0-9a-f]+")

  And the obligatory LPeg version:

lpeg = require "lpeg"

number = lpeg.P"0" * lpeg.S"Xx" * lpeg.R("09","AF","af")^1 * lpeg.Cc("hex")
       + lpeg.R"09"^1 * lpeg.Cc("integer")

print(number:match "0x123")
print(number:match "123")

  -spc