See Substitution outside of HTML TAGS for a previous answer to this question. @open = `cat temp.html`; Perl has perfectly good functions for opening and reading files that don't require you to fork another shell.
foreach (@open) { $_ =~ s/\n//ig;
You don't need the '$_ =~' here. Substitution operates on $_ by default. Also, this is an occassion where tr/// would be a better choice of operators. if ( "$_" eq "<(.*)>" ) { You need a regex here instead of stringwise equality, but even then it won't do what you expect. Also don't get into the bad habit of quoting scalars. If you want $_, just say $_, not "$_".

Here's one way to do this with regular expressions. Regexes, however, invariably fail on "real world" HTML. The prefered method is to use HTML::Parser or its derivatives.

#!/usr/bin/perl -w use strict; open HTML, "temp.html" or die "Can't open file: $!\n"; { local $/; $_ = <HTML>; } close HTML; tr/\n / /s; while ( /([^<>]*)(<[^>]*>)?/g ) { print "TEXT: $text\n" if defined $1; print "HTML: $html\n" if $2; }

In reply to Re: Search and replace everything except html tags by takshaka
in thread Search and replace everything except html tags by thatguy

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.