Mid_Ager has asked for the wisdom of the Perl Monks concerning the following question:

Please be patient to this simple regex memory question: during parsing & remembering the var. (using $1, $2), I cannot get the right syntax for this - <Id>3456</Id> <Name>Scott Susan</Name> ..... Using this - m#(<Id>)(.*)(</Id>)#; print "1 - $1"; ... won't work! Anyone can shed some light on this? Thanks.

Replies are listed 'Best First'.
Re: Regular Expression memory
by DamnDirtyApe (Curate) on Nov 21, 2002 at 22:52 UTC

    What exactly isn't working?

    my $str = '<Id>3456</Id>' ; $str =~ m#(<Id>)(.*)(</Id>)# ; print "1 - $1\n2 - $2\n3 - $3\n" ; [ doug 14:49 ~ ]% perl test.pl 1 - <Id> 2 - 3456 3 - </Id>

    BTW, if you're trying to parse XML, take a look at XML::Simple; it does a fine job.


    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
      Thanks all replies. I figure out the problem - it's because the memory variables - $1,$2, still retains it's old value in next loop (while(<>) ) and doesn't show the correct "values"! Thanks again.
Re: Regular Expression memory
by chromatic (Archbishop) on Nov 22, 2002 at 00:58 UTC

    What happens if the match fails? You need an if in there.

    if (m|(<Id>)(.*?)(</Id>)|) { print "1 - '$1'\n2 - '$2'\n3 - '$3'\n"; } else { print "Hmm, no match. Is this what happened?\n"; }