开发者

How to parse this particular html?

I have the following string:

<span class="ClassName @variable" title="ClassName @variable">Variable Title</span>

"ClassName", "variable" & "Variable 开发者_如何学运维Title" are paramerters.

Now I want to extract the "ClassName", "variable" and "Variable Title" from that sentence. How can I do that?


Javascript:

var matches = /<span class="(.*)" title="(.*)">(.*)<\/span>/.exec(str);

Where str is your tag.

Then...

matches[1]=class
matches[2]=title
matches[3]=tag content

Note that you should really use a proper HTML parser for this kind of thing rather than Regex but never mind :)


Here is a Perl solution:

#!/usr/bin/perl
use 5.10.1;
use strict;
use warnings;
use Data::Dumper;

my $str = q!<span class="ClassName @variable" title="ClassName @variable">Variable Title</span>!;
my @list = $str =~ m#<span class="(\w+) @(\w+).*?>([\w\s]+)</span>#;

say Dumper \@list;

Output:

$VAR1 = [
          'ClassName',
          'variable',
          'Variable Title'
        ];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜