if (my $list = $line =~ /\(\d+(,\d+)*\)/) { ... }

The problem with this is it only captures the match success status in the  $list scalar:

c:\@Work\Perl>perl -wMstrict -le "my $line = '[AHB_REPORTER][INFO]: action(62,1,0,0,0,0,5,53,9,0,190)D:/XYZ/reg/ +Tests/Mcu/A_test.cCALL: (null)'; if (my $list = $line =~ /\(\d+(,\d+)*\)/) { print qq{'$list'}; } " '1'
Because of the way something like  (,\d+)* works, changing  $list to an array  @list isn't much better:
c:\@Work\Perl>perl -wMstrict -le "my $line = '[AHB_REPORTER][INFO]: action(62,1,0,0,0,0,5,53,9,0,190)D:/XYZ/reg/ +Tests/Mcu/A_test.cCALL: (null)'; if (my @list = $line =~ /\(\d+(,\d+)*\)/) { print qq{(@list)}; } " (,190)
(This works the same with or without a  /g modifier on the  m// match.)

To extract all digit groups, you could do something like:

c:\@Work\Perl>perl -wMstrict -MData::Dump; -le "my $line = '[AHB_REPORTER][INFO]: action(62,1,0,0,0,0,5,53,9,0,190)D:/XYZ/reg/ +Tests/Mcu/A_test.cCALL: (null)'; if (my @list = $line =~ m{ (?: \G , | action\( ) (\d+) }xmsg) { printf qq{'$_' } for @list; print ''; my %hash = do { my $k = 'a'; map { $_ ? ($k++ => $_) : () } @list +}; dd \%hash; } " '62' '1' '0' '0' '0' '0' '5' '53' '9' '0' '190' { a => 62, b => 1, c => 5, d => 53, e => 9, f => 190 }
(Add  \s* whitespace flavoring to taste.) (Update: The  \G , pattern assumes that a  , (comma) never occurs at the beginning of  $line.)

Update: If you want to get a bit fancy, do it all in one swell foop and then just test if the hash has anything in it:

c:\@Work\Perl>perl -wMstrict -MData::Dump -le "my $line = '[AHB_REPORTER][INFO]: action(62,1,0,0,0,0,5,53,9,0,190)D:/XYZ/reg/ +Tests/Mcu/A_test.cCALL: (null)'; ;; my %hash = do { my $k = 'a'; map { $_ ? ($k++ => $_) : () } $line =~ m{ (?: \G , | action [(] ) \K \d+ }xmsg; }; ;; if (%hash) { dd \%hash; } else { print 'no got'; } " { a => 62, b => 1, c => 5, d => 53, e => 9, f => 190 }
(The  \K regex operator comes with Perl versions 5.10+. If your version pre-dates 5.10, let me know and I'll supply a simple fix.)


In reply to Re^2: Extract a small part of a long sentence using regular expressions by AnomalousMonk
in thread Extract a small part of a long sentence using regular expressions by swatzz

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.