Your code captures and assigns to $max_id only the last one of the first group of digits found. I expect that's probably not what you wanted.
perl -Mstrict -W -E 'say $1 if "hello 0 and 123 goodbye" =~ /(\d)+/;'
0
$ perl -Mstrict -W -E 'say $1 if "hello and 123 goodbye" =~ /(\d)+/;'
3
Perhaps you want:
$ perl -Mstrict -W -E 'say $1 if "hello and 123 goodbye" =~ /(\d+)/;'
123
Hope this helps!
(Update: show suggested fix and additional failing example)
The way forward always starts with a minimal test.