Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with pattern matching

Status
Not open for further replies.

dipti1

Programmer
Jul 25, 2006
9
US
Hi,

I need to extract the word within < > and </ and > and save it to a variable. For example, <name> and </name>.
The text file has such patterns in it. I need to extract the string from those tags. I used pattern matching as under:
while(my $line1 = <FILE1>){ # for mathing <name>
if($line1 =~ /<\w+>/)
{
$words1[$i] = $1;
print "$1\n";
$i++;
}
}

and

while(my $line2 = <FILE2>){# for </name>
if($line2 =~ /<\w+\/>/)
{
$words2[$j] = $1;
print "$1\n";
$j++;
}
}

But this does not seem to give or print the correct words. I am wrong somewhere. Can anyone help with this regular expression pattern matching ?

Thanks in advance,
Priyanka
 
You maybe should review regular expressions and how they work. You need to use capturing parenthesis in your regexps:

Code:
while(my $line1 = <FILE1>){ # for mathing <name>
    if($line1 =~ /<[red]([/red]\w+[red])[/red]>/)
    {
        $words1[$i] = $1;
        print "$1\n";
        $i++;
    }
}



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Also, assuming you are reading XML, don't try to do it yourself. Save yourself a world of hurt and use one of the XML parsers available on CPAN instead. XML::Simple might work for simple structures, if not XML::parser with a style of Tree or Stream might do what you need.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top