Esteemed Monks,

I'm having a conundrum with a regexp for an HTML background-color attribute. The IDE I'm using highlight's the '#' and subsequent characters as comments. I've tried escaping the hash symbol, to no avail.

Cheers in advance, folks.
Here's the HTML tag I want to change: <body style="background-color: #FAF519">
...and the script...
#!/usr/bin/perl # change background color.plx # Program will read in an html file, change background color and print + to source file. # 1. No need for file variable yet: open (INFILE, "<".$htmlFile) or di +e("Can't read source file!\n"); # /background-color:\s*#([0-9a-f]{6});?/ig # /background-color:\s*#([0-9a-f]{6}|[0-9a-f]{3});?/ig use warnings; use diagnostics; use strict; # Declare and initialise variables. my @htmlLines; # Open HTML test file - forward slashes do not need to be escaped. open INFILE, "E:/Documents and Settings/Richard Lamb/My Documents/HTML +/dummy1.html" or die "Sod! Can't open this file.\n"; # Assign to an array/list variable. @htmlLines = <INFILE>; close (INFILE); sub changeBackColour { foreach my $line (@htmlLines) { # case insensitivity and global search for pattern $line =~ s/background-color:\s*\#([0-9a-f]{6}|[0-9a-f]{3});?/backg +round-color:\s*\#FFFFFF;?/ig; } } changeBackColour(); sub printHTML { for my $i (0..@htmlLines-1) { print $htmlLines[$i]; } } printHTML(); # prints the reformatted HTML file in DOS window

In reply to Escaping '#' in a regexp by Tricky

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.