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.


In reply to Re: regexp identify variable number of digits within a sentence by perlpie
in thread regexp identify variable number of digits within a sentence by bdalzell

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.