Okay, putting aside the arguments that HTML is rarely well formed and that regexps aren't the answer when you can't guarantee the purity of your data etc (ie use HTML::Parser),
an answer to your question is as follows:
$_ = qq{<html><foo>4~<font color="#FF0000"><b>,</b></font><fush>
5~
<fish>#555555<>6~ <font #444444>};
print "$_\n";
my %colours = (m!(\d+)~\s*<\s*font\s+[^#]*(#[A-F0-9]{6})!igs);
print $colours{4}; # will print #FF0000
This code will handle (some cases where) '#' appears elsewhere and will ignore any case where the colour does not contain 6 hex digits. It's case insensitive, and allows spaces all over the place. I still hope that you have some idea of your expected data, as there are many cases that this can still break.
Hope it helps.
jarich
Update: mdillon pointed out a copy error. Was @colours, needed to be %colours.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.