Steve Sarapata wrote: > Hell-o gurus, > > First, as a lurker on this listserve I've seen some amazing code come > through. Keep it up. Someday too I may be able to join the ranks of the > enlightened. > > Second, I work in a Windows shop with a hoard of VB.NETers being the > lone PERLer. > > I have two issues I think can be resolved using a regExp but need a push > in the right direction to find a solution. The problem can best be > explained using an XML example. > > Question 1. I'm looking for the data between an opening and closing tag. > I think this somehow can be written as: > $result = regularExpression; > In the code below I am looking for "aValue". I have only had success > using index-index-substr. This is PERL, there must be a better way. > > Question 2. After extracting data same as above I want the last entry in > a colon delimited string. In the code below I am looking for "last". > Doing index-index-substr-split does the job but this just isn't elegant > enough. > > Any suggestions or ideas to these questions so I can demonstrate the > power of PERL to the masses? > > TIA, > Steve > > > > > ## begin code > use strict; > use warnings; > > my ($blob) = > "<record><tag1>aValue</tag1><tag2>aa:bb:cc:last</tag2></record>";
$blob =~ /tag1>([^<]+)<\/tag1>/; print $1, "\n"; > my ($p1, $p2, $result, @data); > > #question 1 > $p1 = index($blob, "<tag1>"); > $p2 = index($blob, "</"); > $result = substr($blob, ($p1+6), ($p2-($p1+6))); > print $result,"\n"; > > #question 2 > $p1 = index($blob, "<tag2>"); > $p2 = index($blob, "</",$p1); > $result = substr($blob, ($p1+6), ($p2-($p1+6))); > @data = split(/:/,$result); > $result = $data[$#data]; > print $result,"\n"; > ## end code $blob =~ /tag2>[^<]+:([^:]+)<\/tag2>/; print $1, "\n"; -- ,-/- __ _ _ $Bill Luebkert Mailto:[EMAIL PROTECTED] (_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED] / ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/ -/-' /___/_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers stuff) _______________________________________________ ActivePerl mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
