We Recommend

Mastering Regular Expressions Mastering Regular Expressions
As this book shows, a command of regular expressions is an invaluable skill. Regular expressions allow you to code complex and subtle text processing that you never imagined could be automated. Regular expressions can save you time and aggravation. They can be used to craft elegant solutions to a wide range of problems. Once you've mastered regular expressions, they'll become an invaluable part of your toolkit. You will wonder how you ever got by without them.


Posted By

Corhol on 11/20/06


Tagged

regexp hexadecimal


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

copyleft
wbowers


Hexadecimal Value


Published in: Regular Expression 


Matches any hex value (0123456789ABCDEF) preceded by the pound sign (#) of exactly 3 or 6 digits in length.


  1. /(#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})\b)/

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: CUViper on November 23, 2006

I don't think that's the same thing, priyanhere. From the perlre manpage:

{n,m} Match at least n but not more than m times

Thus your example would also match 4 or 5 digits. Since we're looking for exactly 3 or 6, and those happen to be multiples, you could try this:

/(#([0-9A-Fa-f]{3}){1,2}\b)/
Posted By: priyanhere on November 21, 2006

This is good, but below is more efficient /(#([0-9A-Fa-f]{3,6})\b)/

You need to login to post a comment.