Pages

Thursday, July 19, 2012

Determine a Shipper from the Tracking Number


    //from http://www.beginnercode.com/2010/11/09/regex-for-common-courier-tracking-numbers/
    ///****
    //[ UPS ]
    //9 digits, or 1Z+whatever digits
    //The quick brown fox 1Z9999W99999999999 jumps over the lazy dog.
    //*/
    //$ups = '/(\b\d{9}\b)|(\b1Z\d+\b)/';

    ///****
    //[ Fedex ]
    //12 digits, 15 digits, or '96'+ 20 digits
    //The quick brown fox 999999999999 jumps over the lazy dog.
    //*/
    //$fedex = '/(\b96\d{20}\b)|(\b\d{15}\b)|(\b\d{12}\b)/';

    ///***
    //[ USPS ]
    //30 digits, '91'+20 digits, 20 digits (untested)
    //< TOTALLY UNTESTED BY ME AT THIS TIME >
    //*/
    //$usps = '/(\b\d{30}\b)|(\b91\d+\b)|(\b\d{20}\b)/';

    enum shipper { FedEx, UPS, USPS, unknown }


    class ShippingClass
    {
        //[ UPS ]
        //9 digits, or 1Z+whatever digits
        const string UPS_REGEX = @"(^1Z\w+)|(\b\d{9}\b)";
        //[ Fedex ]
        //12 digits, 15 digits, or '96'+ 20 digits
        const string FEDEX_REGEX = @"(\b\d{12}\b)|(\b\d{15}\b)|(^96\d+)";

        //[ USPS ]
        //30 digits, '91'+20 digits, 20 digits (untested)
        const string USPS_REGEX = @"(\b\d{30}\b)|(\b91\d+\b)|(\b\d{20}\b)";

        public static shipper getShipper(string TrackingNumber)
        {
            shipper answer = shipper.unknown;
            if (Regex.IsMatch(TrackingNumber, USPS_REGEX)) { answer = shipper.USPS; }

            if (Regex.IsMatch(TrackingNumber, FEDEX_REGEX)) { answer = shipper.FedEx; }

            if (Regex.IsMatch(TrackingNumber, UPS_REGEX)) { answer = shipper.UPS; }
            return answer;
        }
    }


...

Bryan Valencia is a contributing editor and founder of Visual Studio Journey.  He owns and operates Software Services, a web design and hosting company in Manteca, California.

2 comments:

Dave Bates said...

I'm getting 94 now to start usps, so it seems they are starting to encroach on the fedex space. BAD!

Unknown said...

Probably a result of commingling, because Smartpost

Share This!

Contact Us

Name

Email *

Message *