in reply to Add tag
This looks for a NEW block followed by something (. = match all chars, + = match 1 or more times). Next there may be a < (? = 0 or 1 matches) and then we expect a newline and a [ (beginning of the next OLD or NEW block).$Data = <DATA>; $Data =~ s/(\[NEW\].+?)\<?(\n\[)/$1<p>$2/sg;
All this assumes Linux, for Windows newlines, add a \r before the \n.
If you got more data than you would like to keep and process in RAM, it's a little bit longer, you need to keep the last line before printing:
This loops through your lines and remembers the current line.#!/usr/bin/perl while(<DATA>){ if (/^(\[\w+\])/) { # New block starts $line =~ s/\<$/<p>/ if $f eq 'NEW'; # Replace the < by <p> for NEW blocks only $f = $1; } defined($line) and print $line; # Print the last line $line = $_; # keep for next run # Save this line } print $line; # Print last line __DATA__ [NEW] Dear Carolyn: My husband and I divorced after 42 years. It didn't tak +e long for him to find a replacement, but I was happy for him at the time.< them and let my grandchildren dig through her purse as if she were the +ir grandmother.< [OLD]
$line =~ s/\<$//; # Remove < $line .= '<p>' if $f eq 'NEW'; # Add <p> for NEW blocks only
(I didn't try this code myself, so there may be typos.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Add tag
by Anonymous Monk on Sep 01, 2009 at 16:04 UTC |