Why does my regex fail when the number ends in 0?

Posted by Russell C. on Stack Overflow See other posts from Stack Overflow or by Russell C.
Published on 2010-04-17T16:53:54Z Indexed on 2010/04/20 17:43 UTC
Read the original article Hit count: 235

Filed under:
|

This is a really basic regex question but since I can't seem to figure out why the match is failing in certain circumstances I figured I'd post it to see if anyone else can point out what I'm missing.

I'm trying to pull out the 2 sets of digits from strings of the form:

12309123098_102938120938120938
1321312_103810312032123
123123123_10983094854905490
38293827_1293120938129308

I'm using the following code to process each string:

if($string && $string =~ /^(\d)+_(\d)+$/) {
    if(IsInteger($1) && IsInteger($2)) { print "success ('$1','$2')"; }
    else { print "fail"; }
}

Where the IsInterger() function is as follows:

sub IsInteger {
    my $integer = shift;
    if($integer && $integer =~ /^\d+$/) { return 1; }
    return;
}

This function seems to work most of the time but fails on the following for some reason:

1287123437_1268098784380
1287123437_1267589971660

Any ideas on why these fail while others succeed? Thanks in advance for your help!

© Stack Overflow or respective owner

Related posts about perl

Related posts about regex