Solved

pattern matching help

Posted on 2003-11-29
6
314 Views
Last Modified: 2010-03-04
i need help parseing an xml type:

$item =~
<allInfo>
    <item>
       ITEM A
    </item>
    <item>
       ITEM B
    </item>
</allInfo>
~;

i want to create an array of all the items @items of ITEM A and ITEM B.

0
Comment
Question by:mattaustin
  • 2
  • 2
  • 2
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 9844327
use XML::Parser;
$item = q~
<allInfo>
    <item>
       ITEM A
    </item>
    <item>
       ITEM B
    </item>
</allInfo>
~;
our $ic;
our @items;
sub ch{
}
sub ic{
    my ($p, $el) = @_;
    $ic .= $el;
}
sub sh{
    my ($p, $el, %atts) = @_;
    $p->setHandlers('Char' => \&ic) if ($el eq 'item');
}
sub eh{
    my ($p, $el) = @_;
    if ($el eq 'item' ){
        push @items,$ic;
        $ic = '';
        $p->setHandlers('Char' => \&ch)
    }
;
}
$p = new XML::Parser(Handlers => {Start => \&sh,
                                  End   => \&eh,
                                  Char  => \&ch,
                                  });
$p->parse($item);
print "@items";
0
 
LVL 3

Accepted Solution

by:
terageek earned 100 total points
ID: 9851716
A simpler (although not as elegent) way,

@items = $item =~ /<item>\s*(.*?)\s*<\/item>/gm;
0
 
LVL 84

Expert Comment

by:ozo
ID: 9854833
you mean /s, not /m
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 3

Expert Comment

by:terageek
ID: 9854931
You're right.  I meant /gs not /gm.
0
 
LVL 2

Author Comment

by:mattaustin
ID: 9855334
sub parse{
my @ret;
($string, $tag) = @_;
       while($string =~ m/<$tag>(.*?)<\/$tag>/sg){
       push(@ret,$1);
       }
return @ret;
}
0
 
LVL 2

Author Comment

by:mattaustin
ID: 9855339
thats what i ended up useing, and that ans seemed to be the closest thanks all.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question