[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl-AnyPerl] substituting on multiple lines



On Thu, Jul 06, 2000 at 08:05:13PM +0200, allan juul wrote:

> #!/pack/collect/bin/perl
> 
> open(MAINLATE, "somefile.html");
> @whatever = <MAINLATE>;
> close(MAINLATE);
> 
> foreach $line (@whatever)
> 
>      {
>       $line =~ s/<\/?style>//ig;
>       $line =~ s/^[\s|\t]*\n//ig;

That second one should be:

$line =~ s/^\s*\n//g;

or

$line =~ s/^\s*$//g;


Inside a character class, | is not a metacharacter, so [\s|\t] matches
whitespace or the | character.  \s includes \t, so using both isn't
necessary.


>      }
> 
>  print @whatever
> ______________
> 
> this should replace the two html <style> tags with nothing and remove
> blank lines - no problem.
> what if i want to replace everything in between as well?
> so i end up with a somefile.html that look like:
> 
> <html>
> </html>

First you want to read the whole file into a scalar:

{
  local $/;
  $text = <MAINLATE>;
}

Then you can replace everything between <STYLE> and </STYLE>:

$text =~ s!<STYLE>.*</STYLE>!!i;

And then you can print out the modified string:

print $text;


Ronald

==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-anyperl-request@macperl.org