in reply to Re: Regular Expressions
in thread Regular Expressions

You are using a greedy regex there, and s/<body[^>]*>/$1$blah/gi; will probably grab more than you want.

s/<body[^>]*?>/$1$blah/si;

This would be sufficient, and with the s modifier, it will account for <body> spanning multiple lines. I didn't use g, as I doubt you want to match <body> on a global scale.

Peace

Replies are listed 'Best First'.
Re^3: Regular Expressions
by mrborisguy (Hermit) on Jun 20, 2005 at 13:25 UTC

    I don't understand why this would grab more than what is wanted. It seems to me that the [^>]* will grab everything that isn't a '>', so it'll grab stuff until we finally get to the first instance of '>'. Even though it is greedy, I don't think it would grab past the '>' of the '<body ... >' tag. Care to expand?

    $ perl -e 'my $str = "<body something=\"yep\"><a href=\"..\">"; $str = +~ s/<body[^>]*>/<body>/; print "$str\n"' <body><a href=".."> $

        -Bryan