Perl 去掉字符串中最后的URL名称
我需要去掉变量中的最后一页名称 这是到目前为止我的代码,但我失去了.jpg扩展名,我需要保留它,需要的结果是:ImageName256.jpgPerl 去掉字符串中最后的URL名称,perl,cgi,Perl,Cgi,我需要去掉变量中的最后一页名称 这是到目前为止我的代码,但我失去了.jpg扩展名,我需要保留它,需要的结果是:ImageName256.jpg #!/usr/bin/perl print "Content-type: text/html\n\n"; use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); $Variable = "http://www.MyDomain.com/Somefold
#!/usr/bin/perl
print "Content-type: text/html\n\n";
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
$Variable = "http://www.MyDomain.com/SomefolderPath/ImageName256.jpg";
($LastInUrl) = $Variable =~ m(.*/(\w+));
print $LastInUrl;
如果要继续使用正则表达式,必须使用以下命令:
use URI::Escape qw( uri_unescape );
my $uri = "http://www.MyDomain.com/SomefolderPath/ImageName256.jpg";
my $basename = $uri =~ m{/([^/#?]*)(?=[#?]|\z)} ? uri_unescape($1) : '';
显然,我强烈推荐第一种解决方案,而不是第二种
如果要继续使用正则表达式,必须使用以下命令:
use URI::Escape qw( uri_unescape );
my $uri = "http://www.MyDomain.com/SomefolderPath/ImageName256.jpg";
my $basename = $uri =~ m{/([^/#?]*)(?=[#?]|\z)} ? uri_unescape($1) : '';
显然,我强烈推荐第一种解决方案,而不是第二种解决方案。对于这类问题,我通常避免使用正则表达式,并按如下方式处理:
#!/usr/bin/perl
use strict;
use warnings;
use URI;
use URI::Escape qw( uri_unescape );
use File::Basename;
my $variable = "http://www.MyDomain.com/SomefolderPath/ImageName256.jpg";
my $last_in_url = uri_unescape( basename( URI->new( $variable )->path ) );
print $last_in_url;
注意:File::Basename的行为会根据所使用的系统而变化。如果您想要便携性,您必须使用以下功能:
my $fstype = fileparse_set_fstype('uri');
my $last = uri_unescape( basename( $uri->path ) );
fileparse_set_fstype($fstype);
对于这类事情,我通常避免使用正则表达式,并按如下方式处理:
#!/usr/bin/perl
use strict;
use warnings;
use URI;
use URI::Escape qw( uri_unescape );
use File::Basename;
my $variable = "http://www.MyDomain.com/SomefolderPath/ImageName256.jpg";
my $last_in_url = uri_unescape( basename( URI->new( $variable )->path ) );
print $last_in_url;
注意:File::Basename的行为会根据所使用的系统而变化。如果您想要便携性,您必须使用以下功能:
my $fstype = fileparse_set_fstype('uri');
my $last = uri_unescape( basename( $uri->path ) );
fileparse_set_fstype($fstype);
fileparse\u set\u fstype
没有uri
类型,并且将忽略它无法识别的任何内容,因此fileparse\u set\u fstype('uri')
是不可操作的。改为说fileparse\u set\u fstype(“Unix”)
。事实上,不需要解析路径。在未转义URI路径中的斜杠是明确的,没有引用或转义的路径my($file)=$path=~m{[^/](.*$}
但是@ikegami使用path_segments()的答案更好。@Schwern,这不是一个否定的操作,它与fileparse_set_fstype('Unix')
@ikegami你说得对,我误读了代码。从技术上讲,它是对函数的一种有文档记录的使用,但会愚弄读者,让他们认为它在做它不做的事情。fileparse\u set\u fstype
没有uri
类型,并且会忽略它不识别的任何内容,因此fileparse\u set\u fstype('uri')
是不可操作的。相反,可以说fileparse\u set\u fstype(“Unix”)
。事实上,不需要解析路径。在未转义URI路径中的斜杠是明确的,没有引用或转义的路径my($file)=$path=~m{[^/](.*$}
但是@ikegami使用path_segments()的答案更好。@Schwern,这不是一个否定的操作,它与fileparse_set_fstype('Unix')
@ikegami你说得对,我误读了代码。从技术上讲,这是对函数的一种有据可查的使用,但却愚弄了读者,使他们认为它在做它不做的事情http://example.com/foo将返回foo
但http://example.com/foo/
将返回一个空字符串。既然OP想把文件从末尾取下来,这可能没问题,但我想我会记下来。@Schwern,http://example.com/foo
和http://example.com/foo/
是非常不同的URL。可以预期会返回不同的值。最后一段为空时返回空字符串,这听起来很合适。我同意行为完全正确,只是可能不是OP想要的。警告:http://example.com/foo
将返回foo
但http://example.com/foo/
将返回一个空字符串。既然OP想把文件从末尾取下来,这可能没问题,但我想我会记下来。@Schwern,http://example.com/foo
和http://example.com/foo/
是非常不同的URL。可以预期会返回不同的值。当最后一段为空时返回一个空字符串,这听起来很合适。我同意该行为完全正确,只是可能不是OP想要的。