Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-28 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found