【发布时间】:2013-08-20 03:47:38
【问题描述】:
我有一个 html 页面,其中包含以下网址:
<h3><a href="http://site.com/path/index.php" h="blablabla">
<h3><a href="https://www.site.org/index.php?option=com_content" h="vlavlavla">
我要提取:
site.com/path
www.site.org
在<h3><a href=" 和/index.php 之间。
我试过这段代码:
#!/usr/local/bin/perl
use strict;
use warnings;
open (MYFILE, 'MyFileName.txt');
while (<MYFILE>)
{
my $values1 = split('http://', $_); #VALUE WILL BE: www.site.org/path/index2.php
my @values2 = split('index.php', $values1); #VALUE WILL BE: www.site.org/path/ ?option=com_content
print $values2[0]; # here it must print www.site.org/path/ but it don't
print "\n";
}
close (MYFILE);
但这给出了一个输出:
2
1
2
2
1
1
它不解析 https 网站。 希望你能理解,问候。
【问题讨论】:
-
您在
my $values1 = ...行中拆分$_但此变量没有定义值,除非您在命令行上传递了某些内容。你应该分裂一些你可以积极识别的东西,以了解结果意味着什么。 -
$_由while (<MYFILE>)行设置,这是一个常见的 Perl 习惯用法