Thursday, December 12, 2013

A Quick Regular Expression (Regex) to Pull Colors From a CSS Stylesheet


This one's mostly for my quick personal reference. It wasn't hard by any means but figured someone else might find it useful.

Problem

I need to find all hex color codes in a style sheet so I can extract them.

Solution
I came up with a quick Regex (regular expression) to do this:

#([A-Fa-f0-9]*);

Essentially, it just looks for a pound symbol (#), followed by any amount of numbers 0-9 or letters a-f, followed by a semi-colon.

If you're interested to try it, you can view my example over at Regex Tester.

It's not the slickest but it got the job done quickly.

Happy coding!