How can I extract URLs from plain text with Perl? - regex

I need the Perl regex to parse plain text input and convert all links to valid HTML HREF links. I've tried 10 different versions I found on the web but none of them seen to work correctly. I also tested other solutions posted on StackOverflow, none of which seem to work. The correct solution should be able to find any URL in the plain text input and convert it to:
$1
Some cases other regular expressions I tried didn't handle correctly include:
URLs at the end of a line which are followed by returns
URLs that included question marks
URLs that start with 'https'
I'm hoping that another Perl guy out there will already have a regular expression they are using for this that they can share. Thanks in advance for your help!

You want URI::Find. Once you extract the links, you should be able to handle the rest of the problem just fine.
This is answered in perlfaq9's answer to "How do I extract URLs?", by the way. There is a lot of good stuff in those perlfaq. :)

Besides URI::Find, also checkout the big regular expression database: Regexp::Common, there is a Regexp::Common::URI module that gives you something as easy as:
my ($uri) = $str =~ /$RE{URI}{-keep}/;
If you want different pieces (hostname, query parameters etc) in that uri, see the doc of Regexp::Common::URI::http for what's captured in the $RE{URI} regular expression.

When I tried URI::Find::Schemeless with the following text:
Here is a URL and one bare URL with
https: https://www.example.com and another with a query
http://example.org/?test=one&another=2 and another with parentheses
http://example.org/(9.3)
Another one that appears in quotation marks "http://www.example.net/s=1;q=5"
etc. A link to an ftp site: ftp://user#example.org/test/me
How about one without a protocol www.example.com?
it messed up http://example.org/(9.3). So, I came up with the following with the help of Regexp::Common:
#!/usr/bin/perl
use strict; use warnings;
use CGI 'escapeHTML';
use Regexp::Common qw/URI/;
use URI::Find::Schemeless;
my $heuristic = URI::Find::Schemeless->schemeless_uri_re;
my $pattern = qr{
$RE{URI}{HTTP}{-scheme=>'https?'} |
$RE{URI}{FTP} |
$heuristic
}x;
local $/ = '';
while ( my $par = <DATA> ) {
chomp $par;
$par =~ s/</</g;
$par =~ s/( $pattern ) / linkify($1) /gex;
print "<p>$par</p>\n";
}
sub linkify {
my ($str) = #_;
$str = "http://$str" unless $str =~ /^[fh]t(?:p|tp)/;
$str = escapeHTML($str);
sprintf q|%s|, ($str) x 2;
}
This worked for the input shown. Of course, life is never that easy as you can see by trying (http://example.org/(9.3)).

Here I have posted the sample code using how to extract the urls.
Here it will take the lines from the stdin.
And it will check whether the input line contains valid URL format.
And it will give you the URL
use strict;
use warnings;
use Regexp::Common qw /URI/;
while (1)
{
#getting the input from stdin.
print "Enter the line: \n";
my $line = <>;
chomp ($line); #removing the unwanted new line character
my ($uri)= $line =~ /$RE{URI}{HTTP}{-keep}/ and print "Contains an HTTP URI.\n";
print "URL : $uri\n" if ($uri);
}
Sample output I am getting is as follows
Enter the line:
http://stackoverflow.com/posts/2565350/
Contains an HTTP URI.
URL : http://stackoverflow.com/posts/2565350/
Enter the line:
this is not valid url line
Enter the line:
www.google.com
Enter the line:
http://
Enter the line:
http://www.google.com
Contains an HTTP URI.
URL : http://www.google.com

Related

Perl Regex regular expression to split //

I went through s'flow and other sites for simple solution with regex in perl.
$str = q(//////);#
Say I've six slash or seven, or other chars like q(aaaaa)
I want them to split like ['//','//'],
I tried #my_split = split ( /\/\/,$str); but it didn't work
Is it possible with regex?
Reason for this question is, say I have this domain name:
$site_name = q(http://www.yahoo.com/blah1/blah2.txt);
I wanted to split along single slash to get 'domain-name', I couldn't do it.
I tried
split( '/'{1,1}, $sitename); #didn't work. I expected it split on one slash than two.
Thanks.
The question is rather unclear.
To break a string into pairs of consecutive characters
my #pairs = $string =~ /(..)/g;
or to split a string by repeating slash
my #parts = split /\/\//, $string;
The separator pattern, in /.../, is an actual regex so we need to escape / inside it.
But then you say you want to parse URI?
Use a module, please. For example, there is URI
use warnings;
use strict;
use feature 'say';
use URI;
my $string = q(http://www.yahoo.com/blah1/blah2.txt);
my $uri = URI->new($string);
say "Scheme: ", $uri->scheme;
say "Path: ", $uri->path;
say "Host: ", $uri->host;
# there's more, see docs
and then there's URI::Split
use URI::Split qw(uri_split uri_join);
my ($scheme, $auth, $path, $query, $frag) = uri_split($uri);
A number of other modules or frameworks, which you may already be using, nicely handle URIs.
Here's a quick way to split the full URL into its components:
my $u = q(http://www.yahoo.com/blah1/blah2.txt);
my ($protocol, $server, $path) = split(/:\/\/([^\/]+)/, $u);
print "($protocol, $server, $path)\n";
h/t #Mike
Well next piece of code does the trick
use strict;
use warnings;
use Data::Dumper;
my %url;
while( <DATA> ) {
chomp;
m|(\wttps{0,1})://([\w\d\.]+)/(.+)/([^/]+)$|;
#url{qw(proto dn path file)} = ($1,$2,$3,$4);
print Dumper(\%url);
}
__DATA__
http://www.yahoo.com/blah1/blah2.txt
http://www.google.com/dir1/dir2/dir3/file.ext
ftp://www.server.com/dir1/dir2/file.ext
https://www.inter.net/dir/file.ext
So it seems you want to simply get the Domain name:
my $url = q(http://www.yahoo.com/blah1/blah2.txt);
my #vars = split /\//, $url;
print $vars[2];
results:
www.yahoo.com

Perl regular expression pattern matching the urls

I have an old perl code which I need to improvise it by debugging it in apache server but it has some regular expressions in it which I am not able to figure out exactly as I am new to perl. Could some one please explain what does the following code do?
my $target = " ";
$target = $1 if( $url =~ m|^$shorturl(\/.*)$|);
Here,
url is http://127.0.0.1/test.pl/content/dist/hale_bopp_2.mpg
shorturl is http://127.0.0.1/test.pl
Is extracts the "path info" component of the URL, the extra segments of the path after the path to the script.
http://127.0.0.1/test.pl/content/dist/hale_bopp_2.mpg
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
(It should really be $target = unescape_uri($1) to handle escaped characters.)
From the language perspective, it matches $url with regexp enclosed in m| | and if it matches, put first capture (part of regex in parens) into $target.

perl regex to replace 2 substrings

I wrote a perl snippet that strips http:// and www from the front of a domain name input from the console
#!/usr/bin/perl
use strict;
print "Enter the domain name to be queried:\n";
my $input_domain = <>;
chomp ($input_domain);
my $inter_domain = $input_domain =~ s/http:\/\///r;
my $domain = $inter_domain =~ s/www.//r;
print $domain."\n";
When http://domain-name.tld or http://www.domain-name.tld or even*www.domain-name.tld is entered, this code returns domain-name.tld.
The question I have is, can the same be achieved using a Perl one-liner that combines both the search and replace lines into one?
If you make both the http:// and the www. optional but look for both of them then it will remove either one or both. The only disparity from the original code is that it will change www.http://domain-name.tld to http://domain-name.tld which I think isn't a disadvantage
It seems odd to ask for a on-liner that modifies user input, so I've written this sample that processes four different strings from the DATA file handle. Also note that it's much tidier to use different delimiters for the substitution to avoid having to escape the slashes
use strict;
use warnings;
while ( <DATA> ) {
s|^(?:http://)?(?:www\.)?||;
print;
}
__END__
http://www.domain-name.tld
http://domain-name.tld
www.domain-name.tld
domain-name.tld
output
domain-name.tld
domain-name.tld
domain-name.tld
domain-name.tld
Combine the regex: (http:\/\/)|(www\.)
s/(http:\/\/)|(www\.)//r;
This removes http:// and/or www.

How do I get the host name from a URL in Perl using regex?

so what I want to do is remove everything after and including the first "/" to appear after a "."
so: http://linux.pacific.net.au/primary.xml.gz
would become: http://linux.pacific.net.au
How do I do this using regex? The system I'm running on can't use URI tool.
$url = 'http://linux.pacific.net.au/primary.xml.gz';
($domain) = $url =~ m!(https?://[^:/]+)!;
print $domain;
output:
http://linux.pacific.net.au
and this is the official regular expression can be used to decode a URI:
my($scheme, $authority, $path, $query, $fragment) =
$uri =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|;
I suggest you use URI::Split which will separate a standard URL into its constuent parts for you and rejoin them. You want the first two parts - the scheme and the host.
use strict;
use warnings;
use URI::Split qw/ uri_split uri_join /;
my $scheme_host = do {
my (#parts) = uri_split 'http://linux.pacific.net.au/primary.xml.gz';
uri_join #parts[0,1];
};
print $scheme_host;
output
http://linux.pacific.net.au
Update
If your comment The system I'm running on can't use URI tool means you can't install modules, then here is a regular expression solution.
You say you want to remove everything after and including the first "/" to appear after a ".", so /^.*?\./ finds the first dot, and m|[^/]+| finds everything after it up tot he next slash.
The output is identical to that of the preceding code
use strict;
use warnings;
my $url = 'http://linux.pacific.net.au/primary.xml.gz';
my ($scheme_host) = $url =~ m|^( .*?\. [^/]+ )|x;
print $scheme_host;
The system I'm running on can't use URI tool.
I really recommend doing whatever you can to fix that problem first. If you're not able to use CPAN modules then you'll be missing out on a lot of the power of Perl and your Perl programming life will be far more frustrating than it needs to be.

In Perl, how can I extract email addresses from lines in log files?

I am looking to trim a string that will be created from reading in a file line by line. However i want to pull out only the email from the string, but it will change every time. The only contant is the domain, for example #domain.com.
So for the input string of
00:00:50,004 ERROR [SynchronousCallback] Cannot process resource: test.test#domain.com Channel: channel16
What regular expression will look for #domain.com and pull out all test.test#domain.com. Ive got a regex that will look for the string m/#domain.com/i but i dont know how to then manipulate the string once the #domain.com has been located in the whole string.
The output i would like would be just the email test.test#domain.com
#!/usr/bin/env perl
use strict; use warnings;
use Email::Address;
while (my $line = <DATA>) {
my ($addr) = Email::Address->parse($line);
print $addr->address, "\n";
}
__DATA__
00:00:50,004 ERROR [SynchronousCallback] Cannot process resource: test.test#domain.com Channel: channel16
Output:
C:\temp> tt
test.test#domain.com
Will there always be whitespace immediately preceding the e-mail address? If so, you can use something like:
m/\s([^\s\#]+\#domain.com)/i
Then you can retrieve the whole e-mail address by looking at $1.
If you need all result (more than one email per line) for a regexp you could do this:
while ($str =~ s# ([^ ]+\#domain.com)##i){
my $email = $1;
print $email."\n";
}
regards,
It looks like you simply need to capture all non-whitespace characters preceding the domain string using /\S+\#domain\.com/. This program shows the principle.
my $s = '00:00:50,004 ERROR [SynchronousCallback] Cannot process resource: test.test#domain.com Channel: channel16';
print "$_\n" for $s =~ /\S+\#domain\.com/gi;
output
test.test#domain.com