NightTrainX has asked for the wisdom of the Perl Monks concerning the following question:

I am a relative perl novice so my apologies if I'm asking a really simple question. Your advice is greatly appreciated. I'm tyring to figure out the best way to do the following: open an html file and replace any and all text between a set of span tags with information passed from a form field. I tried to do it via subsitution but my regex skills are pretty weak and it doesn't work. i would like to do it without having to install any custom modules if possible. the span tags in the html file look like this:
<span id=editArea name="whatever"> bla bla a bunch of html and text that will change from file to file </span>
Any wisdom the breathern wish to bestow up me is greatly appreciated. Thanks

Replies are listed 'Best First'.
Re: Open, Find, Replace, Rewrite, Close.. Advice on best path to take
by gav^ (Curate) on Feb 14, 2002 at 16:30 UTC
    Parsing HTML with regexps is error prone. HTML::Parser, HTML::TreeBuilder, etc are great tools. Here is a short example:
    my $str = q{ <span id=editArea name="whatever"> bla bla a bunch of html and text that will change from file to file </span> }; use HTML::TreeBuilder; my $t = HTML::TreeBuilder->new_from_content($str); foreach ($t->look_down('_tag', 'span')) { next if $_->attr('id') ne 'editArea' || $_->attr('name') ne 'whate +ver'; $_->delete_content; $_->push_content('this is some replacement text'); } print $t->as_HTML; $t->delete;

    gav^

Re: Open, Find, Replace, Rewrite, Close.. Advice on best path to take
by rdfield (Priest) on Feb 14, 2002 at 16:13 UTC
    I guess there'll be quite a few answers to this but here's my attempt:
    $newtext = "some new text"; $spantext =~ s/(\<span[^>]+\>)[^<]+(<.*>)/$1${newtext}$2/i; #assuming there's no tags between the spans...

    rdfield

    update
    running

    $_='7065726c646f63207065726c7265';system pack"H*",$_
    might help too :)