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

im a newbie to perl and i want to extract tags from the following:

[check-->PERLMONKS_001] [check-->PERLMONKS_002] [check-->PERLMONKS_003]

The o/p should be

PERLMONKS_001 PERLMONKS_002 PERLMONKS_003

The snippet im trying is

if($array[$i] =~ /Check-->\s*\d+$/) { $Output= $1;

i dont know where im making mistake ..

Replies are listed 'Best First'.
Re: Pattern matching
by Ratazong (Monsignor) on Jan 13, 2012 at 10:57 UTC

    Dear stallion,

    one advantage of perl is that you can try things out pretty fast. So you could just add a

    print "$output\n";
    to your snippet and see whether it works or not. And if it doesn't work at the beginning (it often doesn't), you could use that snippet to iteratively come to a solution. For example you could first try to extract the 3-digit-number.

    One hint: if you want to use $1, you need to capture (*) something from the regular expression. The things you want to capture should be written in round brackets ( ). Extracting the number at the end could look like

    $array[$i] =~ /(\d+)$/;

    HTH, Rata

    * : see perlretut (section on extracting matches).

Re: Pattern matching
by ww (Archbishop) on Jan 13, 2012 at 14:59 UTC

    You appear to be using a C-style loop:

    if($array[$i] =~ /Check-->\s*\d+$/) # ^ C style loop counter?
    Sometimes, that's the (only) way to go, but generally, a more idomatic approach is:

    for my $array(@array) { chomp $array; # may be unnecessary for your aplication if ($array =~ /check-->(\S*\d+)(?:\])$/i) { # \S Not-a-space, / +i case insensitive # ^ also un-needed, for data shown, +but a precaution # against some possible elements in +your __DATA__ my $output = $1; # Good approach; avoids reusing a left-ove +r match
Re: Pattern matching
by hbm (Hermit) on Jan 13, 2012 at 14:11 UTC

    One apparent mistake is that your expression starts with 'C' but your data start with 'c'. Perhaps you want the //i (ignore case) modifier.

Re: Pattern matching
by umasuresh (Hermit) on Jan 13, 2012 at 10:48 UTC
    You are capturing the match, try this untested:
    if($line =~ /check.*\>\s*([^>]+)$/) { print "$1\n"; }
      Yes, the sub-match parenthesis are missing. And the PERLMONKS_ part. And the case of Check. Should be (still not tested):
      if($array[$i] =~ /check-->\s*(\S*\d+)$/) { $Output= $1;