Hoàng Long
Hoàng Long

Reputation: 10848

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?

Upvotes: 1

Views: 140

Answers (2)

Toto
Toto

Reputation: 91518

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'
        ];

Upvotes: 1

El Ronnoco
El Ronnoco

Reputation: 11912

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 :)

Upvotes: 2

Related Questions