470,614 Members | 1,458 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,614 developers. It's quick & easy.

extract text between tags

hi

i have a piece of code

my $text = "<A>ABC</A>";
if ( $text =~ m/ <A>(.*)<\/\1> / )
{
print "text is $text\n";
}

i want to extract "ABC" but could not get it to work. What is wrong with my code.?

thanks
Jul 19 '05 #1
1 10424
On Tue, 16 Mar 2004 at 08:25 GMT, mike wrote:
hi

i have a piece of code

my $text = "<A>ABC</A>";
if ( $text =~ m/ <A>(.*)<\/\1> / ) ^ ^ ^
1 2 1
1. Don't use space here
2. You should also capture what's between the angles for the backreference
to work
{
print "text is $text\n"; ^^^^^
This is probably where you want to print the text you tried to capture
}

i want to extract "ABC" but could not get it to work. What is wrong with my code.?

thanks

So, this will work:

my $text = '<A>ABC</A>';
if ( $text =~ m#<(A)>(.*)</\1># ) {
print "text is $2\n";
}

HTH, Roel
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by mark4 | last post: by
4 posts views Thread by Patrick | last post: by
9 posts views Thread by trihanhcie | last post: by
9 posts views Thread by gregmcmullinjr | last post: by
1 post views Thread by Petra Meier | last post: by
1 post views Thread by Alberto Sartori | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.