in reply to regexp identify variable number of digits within a sentence

When crafting regular expressions, it helps to build off of the more stable parts of the input. In this case, it sounds like there is a ton of variability in the initial and middle parts of the input, but not so much at the end.

I started from the end.

perlpie$ perl -e ' print "A ||118|AVIANN GILDED WILD HONEY. HM 75081701. 02-04-97" =~ /.+?\.\s+(.+)\.\s+[-\d]+$/ ? "id is [$1]\n" : "id not found\n"; ' id is [HM 75081701]

From the end, there's the $ to anchor at the tail, then the \s+[-\d] to gobble up the date of birth, then \.\s+ to get the period and whitespace, then the capture which I've made *very* liberal (.+) and then the period space which precedes it \.\s+ and at the very start a non-greedy .+? to slowly move through the input until the rest of the pattern can match.

Now you could fool that pretty easily. Just stick a dot space in the dog's name and the bit preceding the match will match too soon and you'll get too much in the ID.

perlpie$ perl -e ' print "A ||118|AVIAN. GILDED WILD HONEY. HM 75081701. 02-04-97" =~ /.+?\.\s+(.+)\.\s+[-\d]+$/ ? "id is [$1]\n" : "id not found\n"; ' id is [GILDED WILD HONEY. HM 75081701]

So, we need to be a bit more rigid about the ID.

perlpie$ perl -e ' print "A ||118|AVIAN. GILDED WILD HONEY. HM 75081701. 02-04-97" =~ /.+?\.\s+(\w\w(?:\s+|--)\d+)\.\s+[-\d]+$/ ? "id is [$1]\n" : "id not found\n"; ' id is [HM 75081701]

That works. We're now using \w\w(?:\s+|--)\d+ which matches two word characters followed by (non-capturing parens to contain the alternation) either whitespace or double-dash followed by digits. You can try it with hyphens and the wrong number of digits and a decoy id in the dog's name:

perlpie$ perl -e ' print "A ||118|AVIAN. GILDED WILD HONEY. HM 1234567. HM--75081. 02 +-04-97" =~ /.+?\.\s+(\w\w(?:\s+|--)\d+)\.\s+[-\d]+$/ ? "id is [$1]\n" : "id not found\n"; ' id is [HM--75081]

If you wanted, you could get even more strict about the ID. You could also clean up the id to replace double-hypens with spaces before you do anything else with it.

For more info on perl regular expressions, you'll want to check out perlre.