The code for recognizing delivery company by tracking number (2024)

In this post I'm going to offer you a solution that recognizes a delivery company by tracking number.

All companies use their own tracking number format, so I have created special regular expressions for all of them. I read about formats in the official documentation of delivery companies. This solution works for the following services:

  • UPS
  • USPS
  • FedEX

Formats of tracking numbers

FedEX

The most common tracking number format is 12 digits or 15 digits.
Some other less common formats may also exist, such as 20 digits and 22 digits.

FedEX tracking number examples

9999 9999 9999
9999 9999 9999 999
9999 9999 9999 9999 9999
9999 9999 9999 9999 9999 99
UPS

The most common tracking number format is a combination of 18 alphabetic and numeric characters, usually starting with "1Z". Some other less common formats may also exist.

UPS tracking number examples

1Z 9999 9999 9999 9999
9999 9999 9999
T 9999 9999 99
9999 99999
USPS

The most common tracking number format is 20 digits or a combination of 13 alphabetic and numeric characters, usually starting with 2 alphabets, following by 9 digits, and ending with "US". Some other less common formats may also exist, such as 10 digits.

USPS tracking number examples

USPS Tracking® 9400 1000 0000 0000 0000 00
Priority Mail® 9205 5000 0000 0000 0000 00
Certified Mail® 9407 3000 0000 0000 0000 00
Collect On Delivery Hold For Pickup 9303 3000 0000 0000 0000 00
Global Express Guaranteed® 82 000 000 00

Priority Mail Express International® EC 000 000 000 US
Priority Mail Express® 9270 1000 0000 0000 0000 00
EA 000 000 000 US
Priority Mail International® CP 000 000 000 US
Registered Mail™ 9208 8000 0000 0000 0000 00
Signature Confirmation™ 9202 1000 0000 0000 0000 00

Code for tracking number recognition (regex)

FedEx
^[0-9]{20}$
^[0-9]{15}$
^[0-9]{12}$
^[0-9]{22}$
UPS
^(1Z)[0-9A-Z]{16}$
^(T)+[0-9A-Z]{10}$
^[0-9]{9}$
^[0-9]{26}$
USPS
^(94|93|92|94|95)[0-9]{20}$^(94|93|92|94|95)[0-9]{22}$^(70|14|23|03)[0-9]{14}$^(M0|82)[0-9]{8}$^([A-Z]{2})[0-9]{9}([A-Z]{2})$

Python script for delivery company recognition

import redef recognize_delivery_service(tracking): service = None usps_pattern = [ '^(94|93|92|94|95)[0-9]{20}$', '^(94|93|92|94|95)[0-9]{22}$', '^(70|14|23|03)[0-9]{14}$', '^(M0|82)[0-9]{8}$', '^([A-Z]{2})[0-9]{9}([A-Z]{2})$' ] ups_pattern = [ '^(1Z)[0-9A-Z]{16}$', '^(T)+[0-9A-Z]{10}$', '^[0-9]{9}$', '^[0-9]{26}$' ] fedex_pattern = [ '^[0-9]{20}$', '^[0-9]{15}$', '^[0-9]{12}$', '^[0-9]{22}$' ] usps = "(" + ")|(".join(usps_pattern) + ")" fedex = "(" + ")|(".join(fedex_pattern) + ")" ups= "(" + ")|(".join(ups_pattern) + ")" if re.match(usps, tracking) != None: service = 'USPS' elif re.match(ups, tracking) != None: service = 'UPS' elif re.match(fedex, tracking) != None: service = 'FedEx' return service

PHP class for delivery company recognition

class Tracking{public function getPostServiceName($track){$service = '';if($this->isUSPSTrack($track)){$service = 'USPS';}elseif ($this->isUPSTrack($track)){$service = 'UPS';}elseif ($this->isFedExTrack($track)){$service = 'FedEx';}return $service;}private function isUSPSTrack($track){$usps = array();$usps[0] = '^(94|93|92|94|95)[0-9]{20}$';$usps[1] = '^(94|93|92|94|95)[0-9]{22}$';$usps[2] = '^(70|14|23|03)[0-9]{14}$';$usps[3] = '^(M0|82)[0-9]{8}$';$usps[4] = '^([A-Z]{2})[0-9]{9}([A-Z]{2})$';if (preg_match('/('.$usps[0].')|('.$usps[1].')|('.$usps[2].')|('.$usps[3].')|('.$usps[4].')/', $track)){return true;}return false;}private function isUPSTrack($track){$ups = array();$ups[0] = '^(1Z)[0-9A-Z]{16}$';$ups[1] = '^(T)+[0-9A-Z]{10}$';$ups[2] = '^[0-9]{9}$';$ups[3] = '^[0-9]{26}$';if (preg_match('/('.$ups[0].')|('.$ups[1].')|('.$ups[2].')|('.$ups[3].')/', $track)){return true;}return false;}private function isFedExTrack($track){$fedex = array();$fedex[0] = '^[0-9]{20}$';$fedex[1] = '^[0-9]{15}$';$fedex[2] = '^[0-9]{12}$';$fedex[3] = '^[0-9]{22}$';if (preg_match('/('.$fedex[0].')|('.$fedex[1].')|('.$fedex[2].')|('.$fedex[3].')/', $track)){return true;}return false;}}

To summarize

You can find this code on my GitHub repository related to this topic.

If the format has changed or you have found inaccuracies in this information - please leave a comment below.

I hope this post was helpful and saved time for you.

The code for recognizing delivery company by tracking number (2024)
Top Articles
Latest Posts
Article information

Author: Arline Emard IV

Last Updated:

Views: 5940

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Arline Emard IV

Birthday: 1996-07-10

Address: 8912 Hintz Shore, West Louie, AZ 69363-0747

Phone: +13454700762376

Job: Administration Technician

Hobby: Paintball, Horseback riding, Cycling, Running, Macrame, Playing musical instruments, Soapmaking

Introduction: My name is Arline Emard IV, I am a cheerful, gorgeous, colorful, joyous, excited, super, inquisitive person who loves writing and wants to share my knowledge and understanding with you.