c:\@Work\Perl\monks>perl -wMstrict -le "for my $string (qw( 50.10-d557_5 50.10-d557_8 50.10-d557_20 50.10-d557_123 )) { my ($value) = $string =~ m{ _ (\d+) \z }xms; print qq{'$string' -> '$value'}; } " '50.10-d557_5' -> '5' '50.10-d557_8' -> '8' '50.10-d557_20' -> '20' '50.10-d557_123' -> '123'
Please see perlre, perlretut, and perlrequick. (Update: Also see articles in the Tutorials -> Pattern Matching, Regular Expressions, and Parsing section.)

Update: For completeness, here's code showing capture and use of matching/parsing success status, and also some edge-case and failing parse examples. Data::Dumper::Dumper(), which is core, can be used instead of non-core Data::Dump::dd().

c:\@Work\Perl\monks>perl -wMstrict -e "use Data::Dump qw(dd); ;; for my $string (qw( 50.10-d557_5 50.10-d557_8 50.10-d557_20 50.10-d557_123 _5 ___6 9_99_999_321 999_x 999_7x 999_ 999 _ )) { my $got_value = my ($value) = $string =~ m{ _ (\d+) \z }xms; dd $string, $value, $got_value; if ($got_value) { print qq{'$string' -> '$value' \n\n}; } else { print qq{'$string' parse failed \n\n}; } } " ("50.10-d557_5", 5, 1) '50.10-d557_5' -> '5' ("50.10-d557_8", 8, 1) '50.10-d557_8' -> '8' ("50.10-d557_20", 20, 1) '50.10-d557_20' -> '20' ("50.10-d557_123", 123, 1) '50.10-d557_123' -> '123' ("_5", 5, 1) '_5' -> '5' ("___6", 6, 1) '___6' -> '6' ("9_99_999_321", 321, 1) '9_99_999_321' -> '321' ("999_x", undef, 0) '999_x' parse failed ("999_7x", undef, 0) '999_7x' parse failed ("999_", undef, 0) '999_' parse failed (999, undef, 0) '999' parse failed ("_", undef, 0) '_' parse failed


Give a man a fish:  <%-{-{-{-<


In reply to Re: regex to extract value from a string (updated) by AnomalousMonk
in thread regex to extract value from a string by Anonymous Monk

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.