Skip to content

Instantly share code, notes, and snippets.

@weshoke
Created October 21, 2013 02:06
Show Gist options
  • Select an option

  • Save weshoke/7077684 to your computer and use it in GitHub Desktop.

Select an option

Save weshoke/7077684 to your computer and use it in GitHub Desktop.
Parsing hexadecimal numbers with LPEG
-- Match a hexadecimal digit (lower- or upper-case)
local hexdigit = digit + R("af", "AF")
local hexadecimal = P"0x" * hexdigit^1 * P(-1)
print(hexadecimal:match"0x01") --> '5', match successful
print(hexadecimal:match"0xfa") --> '5', match successful
print(hexadecimal:match"0fa") --> 'nil', match unsuccessful
print(hexadecimal:match"0xfg") --> 'nil', match unsuccessful
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment