in reply to Re: Recognizing 3 and 4 digit number
in thread Recognizing 3 and 4 digit number

G'day Dave,

At first glance, I thought your regex was better than mine and so I decided to try it. I plugged it into my code but it failed on the phone number and date tests (details in spoiler). The OP requirements are not the best but excluding phone numbers and dates seems to be definitely wanted.

— Ken

Replies are listed 'Best First'.
Re^3: Recognizing 3 and 4 digit number
by davido (Cardinal) on Jan 03, 2017 at 17:53 UTC

    Wah! I guess I got excited and answered before noticing that we wanted to disqualify things that look like phone numbers. Sorry.

    This isn't tested:

    m/(?<![\d-])(\d{3,4})(?![\d-])/

    But it would run afoul of phone numbers using commas to separate, or wrapping parens around area codes.

    It might be useful to take a first pass and keep a list of offsets for "numbers" that should be ignored. It's probably easier to match a phone number with existing libraries than to match a 3 or 4 digit number that is not part of a phone number. In other words, on first pass, identify phone numbers, IP addresses, and other problematic numbers, and push their offsets and lengths into an array. Then on second pass disqualify any number that falls within one of the offset/length sets.


    Dave