4

I am trying to pick up regex using nettuts. However I still haven't got very far and something need's fixing today.

I need a regex that does the following.

$html = '...'; // Lots of HTML $regex = '{absolutely anything}color: #{6 digits - [0-9][a-f][A-F]};{absolutely anything}'; 

I will then use this to force users to have a certain color on their HTML elements.

Would anyone mind converting the $regex variable to actual regex?

2
  • 1
    (related) Best Methods to parse HTML - unless you really mean {absolutely anything} Commented Nov 2, 2010 at 14:37
  • {absolutely anything} what would be this value, really? HTML? CSS? Commented Nov 2, 2010 at 14:38

3 Answers 3

3

You're pretty close.

/color:\s*\#[A-Fa-f0-9]{6};/ 
Sign up to request clarification or add additional context in comments.

2 Comments

Not necessary to escape # if you're not using it as a delimiter. Voting on this since it matches 0 to many spaces though and is therefore most flexible ;)
The only difference between mine and @Vincent's above is I allow for 0 or more spaces between the color: and the #. That may or may not be what you want :)
2

If you want "absolutely anything" then... don't add anything else! You can use quantificator and classes for the rest :

$regex = '/color: #[a-fA-F0-9]{6};/' 

[a-fA-F0-9] matches any character between a-f (lowercase), A-F (uppercase) and a digit. {6} means it must has exactly 6 characters.
Don't forget that with PHP's PCRE extension, you need delimiters (/) in this case).

4 Comments

Slightly shorter: /color: #[a-f0-9]{6}/i
nickf > while this is true, the author didn't mention anything about capturing COLOR or such things. But now, he can pick the solution he wants :-p
Using a single (literal) white space makes it error prone when there are zero, or more than one spaces. Better do \s* or \s+, or even [ ]* or [ ]+ when only spaces are allowed and no line breaks.
Bart Kiers > That's your own representation of the problem. The author wants to capture "color: #{something}", he doesn't necessarily want to capture "color:{space}{space}{space} #{something}" or "color:\n#{something}.
2
/color: ?#([0-9a-f]{3}){1,2};/i 

Features:

  • Optional whitespace between color: and value
  • #rgb and #rrggbb matching

Furthermore, you might want to add a [^-] part in order not to match background-color: #...: /[^-]color: ?#([0-9a-f]{3}){1,2};/i. Also, you could use a negative lookbehind if you so desire: (?<!-).

2 Comments

Please correct me if I'm wrong (which is a distinct possibility here), but I don't think PHP supports negative look-behind
Actually I wouldn't have been sure about this. So thanks, @ircmaxell

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.