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

Okay, I know this is a silly question, but I need a little bit of help. I have a string with the value of "1232-32". I want to extract the two right most digits after the minus sign. Can someone give me a hint? I have trouble with regexp quite a bit... Thanks.

Janitored by Arunbear - retitled from 'Newbie needs regexp help'

Replies are listed 'Best First'.
Re: Regex to extract digits
by shenme (Priest) on Oct 09, 2004 at 01:05 UTC
    Okay, the part that is your 'landmark' is the '-'. So you put that in the RE to recognize, but not to 'capture'. You do want to capture the digits, so you use the '\d' (shorthand for [0-9]) to recognize the digits, and surround those with capturing parentheses. So the RE would look like "/-(\d\d)/". But then you want to _use_ it as you said, to get the value, so ...
    my $thing = '1232-32'; my $value; if( $thing =~ m/-(\d\d)/ ) { $value = $1; printf "I found '%s'\n", $value; } else { printf "I didn't find what I was expecting in '%s'\n", $thing; }
    But that would scare many of us because it doesn't check whether there might be more or less than two digits, or whether there are strange characters following the digits, and other such errors. So for the string you showed us, we'd probably feel like saying something like
    if( $thing =~ m/^\d+-(\d+)$/ ) {
    but that might be too much checking for your purposes.
Re: Regex to extract digits
by tachyon (Chancellor) on Oct 09, 2004 at 05:49 UTC

    There are at least three different sensible ways plus a few silly ones.

    $str = "1234-56"; # you can split on the '-' - the second bit is what we want..... my (undef, $digits) = split '-', $str; # you can use index to get the position in the string of the '-' # and substr to get the 2 chars just past that position..... $digits = substr $str, index($str, '-')+1, 2; # you can use an RE..... ($digits) = $str =~ m/\-(\d\d)/;

    cheers

    tachyon

Re: Regex to extract digits
by pg (Canon) on Oct 09, 2004 at 01:29 UTC
    "I want to extract the two right most digits after the minus sign."

    There are different ways to understand what you said, and the example you given might just be one of the pattern you have.

    For example, I could say this meets your requirement:

    use strict; use warnings; my $str = "25364-564756ABCD"; $str =~ /-\d*?(\d{2}).*$/; print $1;

    It would be a good idea to give more examples to cover more of your patterns, if you actuall have.

      This would meet too: /-.*(\d\d)/

      It will match the leftmost - and then greedy match anything up to the last to digits. Surely those two will be the "righ most digits after the minus".

      $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print
Re: Regex to extract digits
by grinder (Bishop) on Oct 09, 2004 at 09:55 UTC
    I want to extract the two right most digits after the minus sign

    A number of regexp examples have been given, but if you know your data, there are simpler ways. If you know the last two characters are the digits:

    my $two = substr($val, -2);

    Or, if you know the last two characters are digits if the third-last character is a dash:

    my $two = substr($val, -2) if substr($val, -3, 1) eq '-';

    - another intruder with the mooring of the heat of the Perl