Created
October 21, 2013 02:06
-
-
Save weshoke/7077684 to your computer and use it in GitHub Desktop.
Parsing hexadecimal numbers with LPEG
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- 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