Perl regular expressions help

Discussion in 'Perl Misc' started by Rumpa, Aug 26, 2005.

  1. Rumpa

    Rumpa Guest

    Hi,
    I am trying to learn perl regular expressions.
    I have:
    my $store = q(
    <store>
    <book>
    <title>abc</title>
    <author>xyz</author>
    </book>
    <book>
    <title>pqr</title>
    <author>mno</author>
    </book>
    </store>);

    I want to extract the titles only in a loop. How can I do this?
    Thanks,
    Rumpa
    Rumpa, Aug 26, 2005
    #1
    1. Advertising

  2. Rumpa

    Paul Lalli Guest

    Rumpa wrote:
    > Hi,
    > I am trying to learn perl regular expressions.
    > I have:
    > my $store = q(
    > <store>
    > <book>
    > <title>abc</title>
    > <author>xyz</author>
    > </book>
    > <book>
    > <title>pqr</title>
    > <author>mno</author>
    > </book>
    > </store>);
    >
    > I want to extract the titles only in a loop. How can I do this?


    Regular expressions are *not* the right tool for parsing XML-like data.
    Please search http://search.cpan.org for the many XML parsing tools.
    If you insist on this as a learning excercise, it would be something
    along the lines of:

    while ($store =~ m{<title>(.*?)</title>}g) {
    print "Title: $1\n";
    }

    For more information:
    perldoc perlretut
    perldoc perlre

    Paul Lalli
    Paul Lalli, Aug 26, 2005
    #2
    1. Advertising

  3. Rumpa <> wrote in news:deo51v$26ui$:

    > I am trying to learn perl regular expressions.


    But parsing XML is best done by using an XML parser.

    > I have:
    > my $store = q(
    > <store>
    > <book>
    > <title>abc</title>
    > <author>xyz</author>
    > </book>
    > <book>
    > <title>pqr</title>
    > <author>mno</author>
    > </book>
    > </store>);
    >
    > I want to extract the titles only in a loop. How can I do this?


    Use an XML parser. You can build on the following example:

    #!/usr/bin/perl

    use strict;
    use warnings;

    use XML::parser;

    my $xml = <<XML
    <store>
    <book>
    <title>abc</title>
    <author>xyz</author>
    </book>
    <book>
    <title>pqr</title>
    <author>mno</author>
    </book>
    </store>
    XML
    ;

    my $in_title;

    my $p = XML::parser->new(Style => 'Stream', Pkg => 'main');
    $p->parse($xml);

    sub StartTag {
    my ($x, $el) = @_;
    return unless $el eq 'title';
    $in_title = 1;
    }

    sub Text {
    my ($x) = @_;
    print "$_\n" if $in_title;
    }

    sub EndTag {
    my ($x, $el) = @_;
    return unless $el eq 'title';
    $in_title = 0;
    }

    __END__





    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Aug 26, 2005
    #3
  4. Rumpa <> wrote:


    > I want to extract the titles only in a loop. How can I do this?



    Use a module that understands XML for processing XML data.


    ---------------------------
    #!/usr/bin/perl
    use warnings;
    use strict;
    use XML::Simple;

    my $store = q(
    <store>
    <book>
    <title>abc</title>
    <author>xyz</author>
    </book>
    <book>
    <title>pqr</title>
    <author>mno</author>
    </book>
    </store>);

    my $content = XMLin $store;

    foreach my $book ( @{ $content->{book} } ) { # perldoc perlreftut
    print "Title is '$book->{title}'\n";
    }
    ---------------------------


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Aug 27, 2005
    #4
    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. Dustin D.
    Replies:
    1
    Views:
    11,144
  2. RN
    Replies:
    0
    Views:
    2,539
  3. Jay Douglas
    Replies:
    0
    Views:
    592
    Jay Douglas
    Aug 15, 2003
  4. codecraig

    Regular Expressions - Python vs Perl

    codecraig, Apr 21, 2005, in forum: Python
    Replies:
    30
    Views:
    5,065
    Ilpo =?iso-8859-1?Q?Nyyss=F6nen?=
    Apr 26, 2005
  5. Noman Shapiro
    Replies:
    0
    Views:
    219
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page