Ramprasad A Padmanabhan wrote:
>
> James Taylor wrote:
> >
> > On Jan 2, 2004, at 5:38 PM, James Taylor wrote:
> >>
> >> I'm trying to parse a bit out of an HTML file, where the formatting
> >> could change daily as far as spaces/newlines go.  Say for example I
> >> have something like this:
> >>
> >> $str=<<EOF;
> >> <html>
> >> <body>
> >> <p>Hello this is juts an example</p>
> >> <p><!---begin---><a href="nowhere.com">blahahahaha</a>
> >> </p><a href="
> >> http://www.somewhere.com";>
> >> HELLO</a>
> >> </p><!---end--->Hello world</body>
> >> </html>
> >> EOF
> >>
> >> $repl="Replacement Text";
> >>
> >> $str =~ s/\<!---begin---\>.+?\<!---end---\>/$repl/im;
> >>
> >>
> >> Well, that doesn't work.
> >>
> >> Any suggestions on this one?  I thought /m was supposed to make
> >> regex's span multiple lines, which seems to be the problem here, as :
> >>
> >> print "good\n" if $str =~ /<!---begin--->.+?<!---end--->/mi
> >>
> >> comes up with nothing, though I am able to match them on their own.
> >> Thanks
> > ok, well I ended up dropping the regex and splitting the strings and
> > re-building them after being inspired by another thread.
> >
> > so,
> >
> > my $str="text <!---begin--->
> > text to
> > be replac
> > ed<!---end--->";
> >
> > my $repl="replacement text";
> > my ($a,$b)=split('<!---begin--->',$str);
> > my ($c,$d)=split('<!---end--->',$b);
> > $str=$a.$repl.$d;
> >
> > I'd still like to know if you can do this via regex however.
> >
>
> put an 's' switch for mulitple line
> s/TEXT/REPLACE/is
>
> do a "perldoc perlre" for better help

The /m modifier says that the string conatains 'M'ultiple records,
so that ^ and $ will match at newlines within the string as well
as just at the ends of the whole string.

The /s modifier says that the string contains a 'S'ingle record
(i.e. newlines are insignificant) so that . will match newline
characters as well as any other character.

Using /m will have no effect in your case as there are no ^ or $ in
your regex. Making this change to your program fixes the problem
as below.

HTH,

Rob


  use strict;
  use warnings;

  my $str=<<EOF;
  <html>
  <body>
  <p>Hello this is juts an example</p>
  <p><!---begin---><a href="nowhere.com">blahahahaha</a>
  </p><a href="
  http://www.somewhere.com";>
  HELLO</a>
  </p><!---end--->Hello world</body>
  </html>
  EOF

  my $repl="Replacement Text";

  $str =~ s/\<!---begin---\>.+?\<!---end---\>/$repl/is;

  print $str, "\n";

**OUTPUT

  <html>
  <body>
  <p>Hello this is juts an example</p>
  <p>Replacement TextHello world</body>
  </html>




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to