我看到一些类似的帖子,但不完全是我想要做的。
我该如何提取和删除URL链接,然后从纯文本中删除它们。
示例:
"Hello!!, I love http://www.google.es".
我希望提取"http://www.google.es",将其保存在一个变量中,然后从我的文本中删除它。
最后,文本应该是这样的:
"Hello!!, I love".
通常情况下,URL是文本的最后一个“单词”,但并非总是如此。
use URI::Find;
my $string = do { local $/; <DATA> };
my $finder = URI::Find->new( sub { '' } );
$finder->find(\$string );
print $string;
__END__
This has a mailto:joe@example.com
Go to http://www.google.com
Pay at https://paypal.com
From ftp://ftp.cpan.org download a file
or use Regexp::Common::URI- provide patterns for URIs.
use strict;
use warning;
use Regexp::Common qw/URI/;
my $str = "Hello!!, I love http://www.google.es";
my ($uri) = $str =~ /$RE{URI}{-keep}/;
print "$uri\n"; #output: http://www.google.es
/((?<=[^a-zA-Z0-9])(?:https?\:\/\/|[a-zA-Z0-9]{1,}\.{1}|\b)(?:\w{1,}\.{1}){1,5}(?:com|org|edu|gov|uk|net|ca|de|jp|fr|au|us|ru|ch|it|nl|se|no|es|mil|iq|io|ac|ly|sm){1}(?:\/[a-zA-Z0-9]{1,})*)/mg
$ cat file
"Hello!!, I love http://www.google.es".
this is another link http://www.somewhere.com
this if ftp link ftp://www.anywhere.com the end
$ awk '{gsub(/(http|ftp):\/\/.[^" ]*/,"") }1' file
"Hello!!, I love ".
this is another link
this if ftp link the end
当然,如果你喜欢的话,也可以将正则表达式适应于Perl
URI::Find
:) - Nikhil Jain