rewriting it a little...with my suggestions and using the whitespace modifier so it's actually readable. :)
s{
(?:^|\G) # start of the last match, so you never backtrack and don't capture.
(?!//) # a section without //
(.*?) # followed by anything
(
http:// # with http://
[^\s]+ # and non-spaces - you could also use \S
)
}
{$1<$2>}xmg;
Trying this in perl, we get:
sub test {
my ($str, $expect) = @_;
my $mod = $str;
$mod =~ s{
(?:^|\G) # start of the last match, so you never backtrack.
(?!//) # a section without //
(.*?) # followed by anything
(
http:// # with http://
[^\s]+ # and non-spaces - you could also use \S
)
}
{$1<$2>}xmg;
print "Expecting '$expect' got '$mod' - ";
print $mod eq $expect ? "passed\n" : "failed\n";
}
test("http://foo.com", "<http://foo.com>");
test("// http://foo.com", "// http://foo.com");
test("foo\nhttp://a.com","foo\n<http://a.com>");
# output is
# Expecting '<http://foo.com>' got '<http://foo.com>' - passed
# Expecting '// http://foo.com' got '// http://foo.com' - passed
# Expecting 'foo
# <http://a.com>' got 'foo
# <http://a.com>' - passed
Edit: Couple of changes: Added the 'm' modifier to make sure that it matches from the start of a line, and change \G to (^|\G) to make sure it starts looking at the start of the line too.