Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   A regular expression query (http://www.velocityreviews.com/forums/t902797-a-regular-expression-query.html)

Devon_Dan 04-23-2007 12:53 PM

A regular expression query
 
First, thanks for taking the time to read this post. I have spent a
while trying to make this work but to no avail and I cant find a post
that explains it. Your time is appreciated.

Now I have a text file with a large amount of XML data in it. I am
trying to extract everything between <Folder>...</Folder> tags. These
extend over a number of lines and there are an undefined number of
these folders in the file. I would like the contents of each of the
folders to be assigned to a either a new variable or part of an array.
Something like:

$result[1] = "contents of folder one";
$result[2] = "contents of folder two"; etc...

I have the contents of the text file as a single variable $content and
I think I have the right regular expression to match the first folder:

$content =~ m/<(F|f)older>t.*?<\/(F|f)older>//s

Still I am unsure how to loop through and collect each of the
instances.

Any tips or hints? Perhaps its a basic hole in my understanding and
someone can even recommend a good tutorial/article that explains it?

Thanks

Dan


Jürgen Exner 04-23-2007 01:03 PM

Re: A regular expression query
 
Devon_Dan wrote:
> First, thanks for taking the time to read this post. I have spent a
> while trying to make this work but to no avail and I cant find a post
> that explains it. Your time is appreciated.
>
> Now I have a text file with a large amount of XML data in it. I am
> trying to extract everything between <Folder>...</Folder> tags.


Any of the XML modules should be able to handle this task easily, e.g.
XML::Simple or XML::Parser

> I have the contents of the text file as a single variable $content and
> I think I have the right regular expression to match the first folder:
>
> $content =~ m/<(F|f)older>t.*?<\/(F|f)older>//s
>
> Still I am unsure how to loop through and collect each of the
> instances.


Maybe you are just looking for the g option (perldoc perlop, 'Regexp
Quote-Like Operators').

> Any tips or hints? Perhaps its a basic hole in my understanding and
> someone can even recommend a good tutorial/article that explains it?


Use the proper tool for the job. REs are not the proper tool for parsing
XML.

jue



Devon_Dan 04-23-2007 01:11 PM

Re: A regular expression query
 
Doh! Thanks for that



All times are GMT. The time now is 02:43 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.