开发者

How can I validate a website URL in Perl?

开发者 https://www.devze.com 2022-12-26 17:01 出处:网络
I need a regul开发者_如何学JAVAar expression or module for validating the website URL using Perl.Regexp::Common::URI::httpI don\'t use regular expressions. I try to create a URI object and see what ha

I need a regul开发者_如何学JAVAar expression or module for validating the website URL using Perl.


Regexp::Common::URI::http


I don't use regular expressions. I try to create a URI object and see what happens. If it works, I have a URI object that I can query to get the scheme (the other things get turned into "schemeless" 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

If I'm looking for a specific sort of URI, I can query the object to see if it satisfies whatever I need, such as a particular domain name. If I'm doing something with URLs, I'm probably going to make an object anyway, so I might as well start with it.


Since you are talking about "a website URL", I guess you are interested in HTTP and HTTPS URLs only.

For that, instead of using regex, you can use the Perl's Data::Validate::URI module.

For example, to validate HTTP and HTTPS URLs:

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)

And, to validate HTTP URL only:

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

Finally, to validate any well-formatted URI:

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

If instead, for any reason, you actually want a regex, then you can use something like the following to validate HTTP/HTTPS/FTP/SFTP URLs:

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


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

精彩评论

暂无评论...
验证码 换一张
取 消