Search code examples
regexperlcut

URL truncated when using regex


I'm trying to extract an URL from an HTTP GET response, so something like "http://xxxxxxxxx.com" has to be extracted.

(https?):\/\/(www\.)?[a-z0-9\.:].*?(?=\s)

But when I get it, using let's say...

$var = "http://www.google.co.uk"
print $var =~ m/(https?):\/\/(www\.)?[a-z0-9\.:].*?(?=\s)/ig; 

The string shown is truncated, like: "http://www.google.com" and nothing else.

Why is this happening?

While using REGEXR the text doesn't cut.


Solution

  • You need to remove the positive lookahead assertion.

    my $var = "http://www.google.co.uk";
    print $var =~ m/https?:\/\/(?:www\.)?[a-z0-9\.:]\S+/ig;