in reply to Re: Extract last two digits from numbers
in thread Extract last two digits from numbers

yosefm,
finding any 4 digit number and capturing its last two digits

Just to be clear, tachyon's regex will match the last two digits of the first set of digits in a string that are at least 4 digits long

  • "The cat 1234567 in the hat"
  • 1234
  • "Just like 3456789 he was 12345"

    Cheers - L~R

    Update: As duff points out below, I am an idiot. My above statement would be correct if the regex had been /\d\d(\d\d)\b/

    • Comment on Re: (2) Extract last two digits from numbers
  • Replies are listed 'Best First'.
    Re: (3) Extract last two digits from numbers
    by duff (Parson) on Jan 06, 2004 at 16:00 UTC
      Just to be clear, tachyon's regex will match the last two digits of the first set of digits in a string that are at least 4 digits long

      That's not clear at all because that's not what his regular expression does. What it does is match the second pair of the first sequence of 4 digits within a string. Here's tachyon's code again: my ($digits)= $str =~ m/\d\d(\d\d)/; and here is what your examples should have looked like:

      • "The cat 1234567 in the hat"
      • 1234
      • "Just lie 3456789 he was 12345"

        duff,
        That's what I get for trying to type what my brain is thinking after spending nearly 36 hours traveling half way around the world with only a few hours sleep. You are most correct. I was thinking about modifying my ^ and $ anchors to \b and somehow saw them in tachyon's code. They are obviously not there and I obviously need more sleep.

        Salamat po (courteous form of thank you in Tagalog)

        L~R