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!
This is an add-on to the answers from unicornaddict and ZyX: what are you trying to match?
If you’re trying to match the sequences left and right of ‘_’, unicorn addict is correct and your regex needs to be
^(\d+)_(\d+)$. Also, you can get rid of the first qualifier and the ‘IsIntrger()` function altogether – you already know it’s an integer – it matched (\d+)If you’re trying to match the last digit in each and wondering why it’s failing, it’s the first check in
IsInteger()(if($intger &&). It’s redundant anyway (you know it’s an integer) and fails on 0 because, as ZyX notes – it evaluates to false.Same thing applies though:
This will output
success ('8','8')given the input12309123098_102938120938120938