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

Fellow Monks:
in this piece of test code:
#!/usr/bin/perl -w use strict; my $longstring="first<!--NEWENTRY889700-->last"; my @entries=split(/<!--NEWENTRY\d\d\d\d\d\d-->/,$longstring); print "$entries[0] $entries[1]\n";
I would like to make use of the string that was matched by the regex in the split code line since the numeric will be changing. Is there a special variable such as $_ (which obviously is not it) that contains the matched value?
Thanks in advance for your help.

Replies are listed 'Best First'.
Re: split/matching/regex question
by Roy Johnson (Monsignor) on Apr 14, 2004 at 20:09 UTC
    I would like to make use of the string that was matched by the regex in the split code
    Then capture it (note the parens below):
    my @entries = split /(<!--NEWENTRY\d{6}-->)/, $longstring
    Now @entries will be interlaced with your delimiter string.

    The PerlMonk tr/// Advocate

      Expanding upon Roy Johnson's example:

      use strict; use warnings; my $longstring = "first<!--NEWENTRY889700-->second third<!--NEWENTRY86 +7530-->last"; my @entries = split /<!--NEWENTRY(\d{6})-->/, $longstring; print "@entries\n";

      prints:

      first 889700 second third 867530 last

      Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"
Re: split/matching/regex question
by kvale (Monsignor) on Apr 14, 2004 at 19:32 UTC
    Split discards the delimiter, so a better solution would be a simple regexp:
    my @entries = $longstring =~ /^([^<]*)<!--NEWENTRY(\d+)-->([^>]*)$/;

    -Mark

      ...but not that simple regexp. He didn't indicate that there was only one delimiter in the string.
      my @entries = $longstring =~ /(.*?)(?:<!--NEWENTRY(\d+)-->)/g;
      This will interleave the data with the numbers from the delimiter.

      Update: Thanks to kvale for pointing out that this drops trailing data. I cannot come up with a foolproof fix using only a pattern match capture. This one will keep all data, but will tack on a phantom (empty) delimiter entry if the line ends with data:

      my @entries = $longstring =~ /(.*?)(?:<!--NEWENTRY(\d+)-->|$)/g;

      The PerlMonk tr/// Advocate
        Good point regarding multiple delimiters, I was just working from his example.

        Your regexp, however, will miss the element after the last delimiter. Your split method in a following node is a better solution.

        -Mark