2005-10-31

Verify a string is a hex number

Eric Gunnerson is doing something I like, probably more than I should. He's posting a regular expression question every Monday, and posting the answer later in the week.

These are quick and easy little problems that I enjoy because I don't work with regexs much and I like the challenge of drafting an effective one in a short amount of time.

For this week's challenge, one is to verify that a string is a hexadecimal number. I came up with this:

^(0[xX])?[0-9a-fA-F]*$

No comments: