I tried running your regex on 2 different lines. The first sample line failed and the second one passed (although its not producing the results I think you want).
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use constant STATUS_COMPLETED_RE => qr{ ^string-here \s string-here: \s \((\w+)\) \s #alarm level ([\w|\s]+) \s completed, \s #job completed Total\s (\d+) \sclient\(s\), \s #total client +s ( #Details - (\d+) \s # number ([\w|\s\(\)]+) ,|\. # su +cceed,fail,etc. )* # any numbe +r of }xms ; my @sample_str = ('string-here string-here: (alarm_level_5) group5 com +pleted, Total 55 client(s),', 'string-here string-here: (alarm_level_2) group2 completed, To +tal 22 client(s), 422 succeed,622 fail,911 no_pass.'); my (@data, %stats); for my $line ( @sample_str ) { if (@data = ($line =~ STATUS_COMPLETED_RE )) { print Data::Dumper->Dump([\@data], [qw/*data/]); $stats{level} = shift @data; $stats{group} = shift @data; $stats{total} = shift @data; } else { print "No match\n"; } } print Data::Dumper->Dump([\%stats], [qw/*stats/]);
Output from running the program was:
C:\perlp>perl try.pl No match @data = ( 'alarm_level_2', 'group2', '22', '622 fail,', '622', 'fail' ); %stats = ( 'group' => 'group2', 'level' => 'alarm_level_2', 'total' => '22' ); C:\perlp>C:\perlp>
The first sample failed because of line:
Total\s (\d+) \sclient\(s\), \s #total client
The \s requires a space even if there isn't a details section that follows. It could be fixed with \s?.

In parsing the details section, notice from the output that @data, only picked up the second detail set.

It did this because it only captures the last successful match. First, it captured 422 succeed,, then 622 fail, and failed to match 922 no_pass (because the line doesn't end in a comma).

Also, it's capturing the whole detail section and the separate 2 items inside the details. I think what you mean (?) is to capture all the detail groups as a whole (i.e., 422, 622 and 911 and their status).

If thats what you mean, then this might be the code:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use constant STATUS_COMPLETED_RE => qr{ ^string-here \s string-here: \s \((\w+)\) \s #alarm level ([\w\s]+) \s completed, \s #job completed Total\s (\d+) \sclient\(s\), \s? #total clien +ts ((?: #Details +- \d+ \s # number [\w\s\(\)]+ [,.] # succe +ed,fail,etc. )*) # any numbe +r of }xms ; my @sample_str = ('string-here string-here: (alarm_level_5) group5 com +pleted, Total 55 client(s),', 'string-here string-here: (alarm_level_2) group2 completed, To +tal 22 client(s), 422 succeed,622 fail,911 no_pass.'); my (@data, %stats); for my $line ( @sample_str ) { if (@data = ($line =~ STATUS_COMPLETED_RE )) { print Data::Dumper->Dump([\@data], [qw/*data/]); $stats{level} = shift @data; $stats{group} = shift @data; $stats{total} = shift @data; } else { print "No match\n"; } } print Data::Dumper->Dump([\%stats], [qw/*stats/]);

The output for this was:

C:\perlp>t1.pl @data = ( 'alarm_level_5', 'group5', '55', '' ); @data = ( 'alarm_level_2', 'group2', '22', '422 succeed,622 fail,911 no_pass.' ); %stats = ( 'group' => 'group2', 'level' => 'alarm_level_2', 'total' => '22' ); C:\perlp>
Update: Here's another way to construct the regex which might be easier to read (from an example in 'Programming Perl', in the chapter Pattern Matching).

my $start = 'string-here string-here:'; my $level = '\((\w+)\)'; my $group = '([\w\s]+) completed,'; my $total = 'Total (\d+) client\(s\),'; # if there are details, they're captured in the end parens my $re = qr/^$start $level $group $total ?(.*)$/;

In reply to Re: Assign regexp output to array not working by Cristoforo
in thread Assign regexp output to array not working by bowei_99

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.