develooper Front page | perl.beginners | Postings from January 2002

Multiple Substitutions with RE on a single line?

Thread Next
From:
Tim Sargent
Date:
January 14, 2002 22:15
Subject:
Multiple Substitutions with RE on a single line?
Message ID:
JIEELDDCEAMLPAGNKPCGIEGLCIAA.corsair@easystreet.com
Hello -

   I'm attempting to develop a simple script that will read a text file and
turn it into an HTML page for me.  It's not CGI - I run the script from the
command line and then upload the resultant file to the web server.

   In a nutshell, within this text file certain words have a specific color
or style assigned to them.  What I want to do is be able to read a line a
time, and make all the changes to the line (if any) without altering the
actual spacing of the lines.  For example:

   Unaltered (straight text) line:
   -------------------------------
   GS5 launches 3 plasma torpedoes (GS5.p1-3), warhead strength 20, speed
32,

   Formatted line:
   ---------------
   <SPAN CLASS="green">GS5</SPAN> <SPAN CLASS="launch">launches</SPAN> 3
plasma torpedoes (<SPAN CLASS="green-atu">GS5.p1-3</SPAN>), warhead strength
20, speed 32,

    Currently I'm using a bunch of IF statements to try and find these
words, but it has difficulty with duplicate words in the same line (which is
a legal occurence for the page), and words that contain other words (like
GS5.p1-3 contains GS5).

    Code sample follows:

    if ($line =~ /\s+([BS]\d{1,2})\s+) {
      $newText = "<SPAN CLASS="blue">$1</SPAN>";
      $line =~ s/$1/$newText/g;
    }

    and so forth.  I'd prefer an efficient and neat method to do all this,
but am rather stuck.  Any pointers that can get me started down the right
path would be greatly appreciated...I know there's an answer out there, I'm
just having a devil of a time finding it.  Sorry for the lengthy post.

    Thanks in advance for your help.

Tim




Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About