[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: String to Hexadecimal Conversion
- From: Sean Conner <sean@...>
- Date: Mon, 27 Oct 2014 17:25:52 -0400
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