in reply to Regular Expressions

Here's one way
$string="<body leftmargin=0 bgcolor=\"red\">"; $newContent = "Test String"; $string =~ s/(<body(.*)>)/$1$newContent/g;
HTH

Replies are listed 'Best First'.
Re^2: Regular Expressions
by davidrw (Prior) on Jun 19, 2005 at 22:52 UTC
    See what i just posted below.. that won't work if the string is something like (and presumably it is, otherwise it would just be $string .= $newContent;):
    my $string =<<EOS; <body leftmargin=0 bgcolor="red" > blah and stuff </body> EOS
      Yes, I understand the limitations. Will be more careful in the future. Apologies to the OP, and thanks to the monks...
      OT: excellent regex point, but OP's html fu could stand some help: leftmargin is/was IE specific (some other browsers honor it; some don't) and arguement '0' should be double quoted.

      And re specific example, space between "red" and > is also naughty... (but I can see how the string could -- real world -- include '\n's, I'm skeptical that spreading it out over multiple lines enhances readability.

      <UPDATE ++ davidrw, below, re use of JS or TT! Again proving adage that one should put brain in gear before opening mouth. Thanks!
        good points.. I almost commented on the double quotes, too.. and y, css can handle the margin stuff..

        As for \n's inside the <body ... > opening tag, here's a couple trivial examples that illustrate the types of cases that i've done that for readability (basically for javascript, and for Template Toolkit stuff):
        <body bgcolor="red" foo="bar" onLoad=" if( x = do_something() ) { do_other_js() ; } else { blah( +); }" > # TT syntax: <body bgcolor="red" foo="bar" [% IF use_js %] onLoad=" ... " [% END %] >