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?

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
ComputerUser
  • 4,828
  • 15
  • 58
  • 71
  • 1
    *(related)* [Best Methods to parse HTML](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662) - unless you really mean *{absolutely anything}* – Gordon Nov 02 '10 at 14:37
  • {absolutely anything} what would be this value, really? HTML? CSS? – Phill Pafford Nov 02 '10 at 14:38

3 Answers3

3

You're pretty close.

/color:\s*\#[A-Fa-f0-9]{6};/
Cfreak
  • 19,191
  • 6
  • 49
  • 60
  • 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 ;) – BoltClock Nov 02 '10 at 14:35
  • 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 :) – Cfreak Nov 02 '10 at 14:36
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).

Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
  • Slightly shorter: `/color: #[a-f0-9]{6}/i` – nickf Nov 02 '10 at 14:35
  • 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 – Vincent Savard Nov 02 '10 at 14:36
  • 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 Nov 02 '10 at 14:36
  • 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}. – Vincent Savard Nov 02 '10 at 14:38
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: (?<!-).

jensgram
  • 31,109
  • 6
  • 81
  • 98
  • Please correct me if I'm wrong (which is a distinct possibility here), but I don't *think* PHP supports negative look-behind – nickf Nov 02 '10 at 16:19
  • Actually I wouldn't have been sure about this. So thanks, @ircmaxell – jensgram Nov 03 '10 at 07:00