$match = q(<a href="#google"><h1><b>Google</b></h1></a>);
if($match =~ /<a.*?href.*?><.?>(.*?)<\/a>/){
$title = $1;
}else {
$title="";
}
print"$title";
Run Code Online (Sandbox Code Playgroud)
OUTPUT: Google</b></h1>
它应该是 : Google
无法使用Perl中的Regex从链接中提取值,它可能会有一个或多或少的嵌套:
<h1><b><i>Google</i></b></h1>
Run Code Online (Sandbox Code Playgroud)
请试试这个:
1)<td> <a href="/wiki/Unix_shell" title="Unix shell"> Unix shell </a>
2)<a href="http://www.hp.com"> <h1> <b> HP </ b> </ h1> </a>
3)<a href="/wiki/Generic_programming" title="Generic programming">通用</a> </ td>);
4)<a href="#cite_note-1"> <span> [</ span> 1 <span>] </ span> </a>
OUTPUT:
Unix shell
生命值
通用
[1]
不要像评论中提到的那样使用正则表达式.我特别喜欢Mojo套件,它允许我使用CSS选择器:
use Mojo;
my $dom = Mojo::DOM->new(q(<a href="#google"><h1><b>Google</b></h1></a>));
print $dom->at('a[href="#google"]')->all_text, "\n";
Run Code Online (Sandbox Code Playgroud)
use HTML::TreeBuilder::XPath;
my $dom = HTML::TreeBuilder::XPath->new_from_content(q(<a href="#google"><h1><b>Google</b></h1></a>));
print $dom->findvalue('//a[@href="#google"]'), "\n";
Run Code Online (Sandbox Code Playgroud)