views:

81

answers:

3

I have a string like this <name>sekar</name>. I want to split this string (i am using perl) and take out only sekar, and push it into an array while leaving other stuff.

I know how to push into an array, but struck with the splitting part.

Does any one have any idea of doing this?

+1  A: 

Try this:

my($name) = $string =~ m|<name>(.*)</name>|;

From perldoc perlop:

If the "/g" option is not used, "m//" in list context returns a list consisting of the subexpressions matched by the parentheses in the pattern, i.e., ($1, $2, $3...).

eugene y
+1  A: 
push @output, $1 if m|<name>(\w*)</name>|;
hlynur
Thanks hlynurIt works perfectly
Senthil kumar
A: 

Try <(("[^"]*"|'[^']*'|[^'">])*)>(\w+)<\/\1>. Should work, when I get home I'll test it. The idea is that the first capture group finds the contents within a <> and its nested capture group prevents a situation like <blah=">"> matching as <blah=">. The third capture group (\w+) matches the inner word. This may have to be changed depending on the format of the possibilities you can have within the <tag>content</tag>. Lastly the \1 looks back at the content of the first capture group so that this way you will find the proper closing tag.

Edit: I've tested this with perl and it works.

stocherilac
Would have been nice to get a justification for a down vote. I still haven't had a chance to check the code yet so if its wrong let me know and I'll make sure to correct it.
stocherilac