A regular expression query

Discussion in 'Perl Misc' started by Devon_Dan, Apr 23, 2007.

  1. Devon_Dan

    Devon_Dan Guest

    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
    Devon_Dan, Apr 23, 2007
    #1
    1. Advertising

  2. 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
    Jürgen Exner, Apr 23, 2007
    #2
    1. Advertising

  3. Devon_Dan

    Devon_Dan Guest

    Doh! Thanks for that
    Devon_Dan, Apr 23, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lonelyplanet999

    Regular Expression Query

    lonelyplanet999, Aug 14, 2003, in forum: Perl
    Replies:
    1
    Views:
    1,808
  2. VSK
    Replies:
    2
    Views:
    2,266
  3. Sriv Chakravarthy
    Replies:
    0
    Views:
    779
    Sriv Chakravarthy
    Sep 18, 2003
  4. Martin Biddiscombe

    Regular expression query

    Martin Biddiscombe, Feb 3, 2006, in forum: Python
    Replies:
    4
    Views:
    360
    bruno at modulix
    Feb 6, 2006
  5. Replies:
    6
    Views:
    647
Loading...

Share This Page