SPUG: Re: Regex multi-line question

Ken Clarke kenslinux at shaw.ca
Tue Aug 3 12:34:17 CDT 2004


This'll get ya started.  Just treat it as a single line.

@results = $XML =~ m|type="([^"]+)"
       .*?
       <path>([^<]+)</path>
       .*?
       <tarname>([^<]+)</tarname>
      |sgx;

while (@results) {
   @extract = splice(@results, 0, 3);
    print join(', ', @extract) . "\n";
}

You are much better off using a parser however since this approach has no
tolerance for eg single quoted type='' in which case the media record would
be skipped, or missing <path> in which case record 1 will grab record 2's
path etc...

>> Ken Clarke
>> Contract Web Programmer / E-commerce Technologist
>> www.PerlProgrammer.net

----- Original Message ----- 
From: "Richard Wood" <wildwood_players at yahoo.com>
To: "Seattle Perl Users Group" <spug-list at pm.org>
Sent: Tuesday, August 03, 2004 9:57 AM
Subject: SPUG: Regex multi-line question


> SPUGsters,
>
> I am always impressed at the demonstrations of regex's
> that are shown at SPUG.
>
> I have been trying to figure out a one liner that will
> parse out multiple sets of patterns from an xml file.
>
> I can do it easily enough with a while<> loop (or a
> parser) but I figure there is a more obfuscated
> solution that one of you can propose using one line.
>
> I am looking for a result something like:
>
> Record, /home/woody/vinyl/Migration, JB_Migration.tar
> CD, /home/woody/cd/Migration, JB_Migration.tar
> TAPE, /home/woody/cass/Migration, JB_Migration.tar
>
> >From the following file.
>
> Don't ask/recommend that I use a parser, that isn't
> the objective.  Think of it as more a perl golf
> question.
>
> <?xml version="1.0" encoding="UTF-8" ?>
> <transfer>
> <media type="Record">
>   <server>MUSIC</server>
>   <path>/home/woody/vinyl/Migration</path>
>   <tarname>JB_Migration.tar</tarname>
> </media>
> <media type="CD">
>   <server>MUSIC</server>
>   <path>/home/woody/cd/Migration</path>
>   <tarname>JB_Migration.tar</tarname>
> </media>
> <media type="TAPE">
>   <server>MUSIC</server>
>   <path>/home/woody/cass/Migration</path>
>   <tarname>JB_Migration.tar</tarname>
> </media>
> </transfer>
>
> Thanks,
>
> Rich Wood
>
> =====
> Richard O. Wood
> Wildwood IT Consultants, Inc.
> wildwood_players at yahoo.com
> 425.281.1914 mobile
> 206.544.9885 desk
>
>
>
> __________________________________
> Do you Yahoo!?
> New and Improved Yahoo! Mail - Send 10MB messages!
> http://promotions.yahoo.com/new_mail
> _____________________________________________________________
> Seattle Perl Users Group Mailing List
> POST TO: spug-list at mail.pm.org  http://spugwiki.perlocity.org
> ACCOUNT CONFIG: http://mail.pm.org/mailman/listinfo/spug-list
> MEETINGS: 3rd Tuesdays, Location Unknown
> WEB PAGE: http://www.seattleperl.org



More information about the spug-list mailing list