28 October 2014

Quiz 43: Find TAG and attributes of HTML code

Problem:
Given a  HTML line, find the tag and its attributes.
ex Name:<input type="text">, here tag is 'input' and attribute is 'type'

Input Format: 
included in code
$n='<a href="http://www.google.com" name="test" id="id1">Click me</a>';

Output Format: 
Attributes for tag a are href & name & id

Constraints: 
none

Sample Input
<a href="http://www.google.com" name="test" id="id1">Click me</a>

Sample Output:
Attributes for tag a are href & name & id

Explanations:
tag is a and others are attributes


Solution:

$n='<a href="http://www.google.com" name="test" id="id1">Click me</a>';
($a)=$n=~/<(.*?)\s/;
(@arr)=$n=~/\s(.*?)=/g;
print "attributes of tag $a are ";
$c=0;
foreach(@arr)
{
if($c>0){print "& "};
print "$_ ";
$c++;
}


Tips:
Tag is anything between < and space. attribute is anything between space and =

No comments:

Post a Comment