How to check website URL in Perl?

I need a regular expression to validate a website URL using Perl.

+3
source share
4 answers
+11
source

I do not use regular expressions. I am trying to create a URI object and see what happens. If this works, I have a URI that I can query to get the schema (other things turn into "inconclusive" URIs).

use URI;

while( <DATA> )
    {
    chomp;
    my $uri = URI->new( $_, 'http' );
    if( $uri->scheme ) { print "$uri is a URL\n"; }
    else               { print "$uri is not a URL\n"; }
    }

__END__
foo.html
http://www.example.com/index.html
abc
www.example.com

URI, , , , , , . - URL-, , , , .

+10
 use Regexp::Common qw /URI/;
    while (<>) {
        /($RE{URI}{HTTP})/       and  print "$1 is an HTTP URI.\n";
    }
+3

"URL- -", , URL- HTTP HTTPS.

Perl Data:: Validate:: URI.

, URL- HTTP HTTPS:

use Data::Validate::URI;
my $url = "http://google.com";
my $uriValidator = new Data::Validate::URI();

print "Valid web URL!" if $uriValidator->is_web_uri($url)

HTTP-:

print "Valid HTTP URL!" if $uriValidator->is_http_uri($url)

, URI:

print "Valid URI!" if $uriValidator->is_uri($url)

- , URL- HTTP/HTTPS/FTP/SFTP - :

print "Valid URL!\n" if $url =~ /^(?:(?:https?|s?ftp))/i;
+2

Source: https://habr.com/ru/post/1740297/


All Articles