Regular Expression patterns for Tracking numbers

余生颓废 提交于 2019-12-29 02:32:49

问题


Does anybody know good place or patterns for checking which company tracking number is the given tracking number for a package. Idea is After scanning a barcode for a package check tracking number with patterns and show which company it was shipped by.


回答1:


Just thought I would post an update on this as I am working on this to match via jquery and automatically select the appropriate shipping carrier. I compiled a list of the matching regex for my project and I have tested a lot of tracking numbers across UPS FedEX and USPS.

If you come across something which doesn't match, please let me know here via comments and I will try to come up for that as well.

UPS:

/\b(1Z ?[0-9A-Z]{3} ?[0-9A-Z]{3} ?[0-9A-Z]{2} ?[0-9A-Z]{4} ?[0-9A-Z]{3} ?[0-9A-Z]|[\dT]\d\d\d ?\d\d\d\d ?\d\d\d)\b/

FedEX: (3 Different Ones)

/(\b96\d{20}\b)|(\b\d{15}\b)|(\b\d{12}\b)/
/\b((98\d\d\d\d\d?\d\d\d\d|98\d\d) ?\d\d\d\d ?\d\d\d\d( ?\d\d\d)?)\b/
/^[0-9]{15}$/

USPS: (4 Different Ones)

/(\b\d{30}\b)|(\b91\d+\b)|(\b\d{20}\b)/
/^E\D{1}\d{9}\D{2}$|^9\d{15,21}$/
/^91[0-9]+$/
/^[A-Za-z]{2}[0-9]+US$/

Please note that I did not come up with these myself. I simply searched around and compiled the list from different sources, including some which may have been mentioned here.

Thanks

Edit: Fixed missing end delimiter.




回答2:


I pressed Royal Mail for a regex for the Recorded Delivery & Special Delivery tracking references but didn't get very far. Even a full set of rules so I could roll my own was beyond them.

Basically, even after they had taken about a week and came back with various combinations of letters denoting service type, I was able to provide examples from our experience that showed there were additional combinations that were obviously valid but that they had not documented.

The references follow the apparently standard international format that I think Jefe's /^[A-Za-z]{2}[0-9]+GB$/ regex would describe:

XX123456789GB

Even though this seems to be a standard format, i.e. most international mail has the same format where the last two letters denote the country of origin, I've not been able to find out any more about this 'standard' or where it originates from (any clarification welcome!).

Particular to Royal Mail seems to be the use of the first two letters to denote service level. I have managed to compile a list of prefixes that denote Special Delivery, but am not convinced that it is 100% complete:

AD AE AF AJ AK AR AZ BP CX DS EP HC HP KC KG
KH KI KJ KQ KU KV KW KY KZ PW SA SC SG SH SI
SJ SL SP SQ SU SW SY SZ TX WA WH XQ WZ

Without one of these prefixes the service is Recorded Delivery which gives delivery confirmation but no tracking.

It seems generally that inclusion of an S, X or Z denotes a higher service level and I don't think I've ever seen a normal Recorded Delivery item with any of those letters in the prefix.

However, as you can see there are many prefixes that would need to be tested if service level were to be checked using regex, and given the fact that Royal Mail seem incapable of providing a comprehensive rule set then trying to test for service level may be futile.




回答3:


I need to verify JUST United States Postal Service (USPS) tracking numbers. WikiAnswers says that my number formats are as follows:

USPS only offers tracking with Express mail, with usually begins with an "E", another letter, followed by 9 digits, and two more letters. USPS does have "Label numbers" for other services that are between 16 and 22 digits long.

http://wiki.answers.com/Q/How_many_numbers_in_a_USPS_tracking_number

I'm adding in that the Label numbers start with a "9" as all the ones I have from personal shipments for the past 2 years start with a 9.

So, assuming that WikiAnswers is correct, here is my regex that matches both:

/^E\D{1}\d{9}\D{2}$|^9\d{15,21}$/

It's pretty simple. Here is the break down:

^E       - Begins w/ E  (For express number)
\D{1}    - followed by another letter
\d{9}    - followed by 9 numbers
\D{2}    - followed by 2 more letters
$        - End of string

|        - OR

^9       - Basic Track & Ship Number
\d{15,21}   - followed by 15 to 21 numbers
$        - End of string

Using www.gummydev.com's regex tester this patter matches both of my test strings:

EXPRESS MAIL : EK225651436US

LABEL NUMBER: 9410803699300003725216

**Note: If you're using ColdFusion (I am), remove the first and last "/" from the pattern




回答4:


Here are some sample numbers from the main US carriers:

USPS:
70160910000108310009   (certified)
23153630000057728970   (signature confirmation)
RE360192014US          (registered mail)      
EL595811950US          (priority express)
9374889692090270407075 (regular)

FEDEX:
810132562702  (all seem to follow same pattern regardless) 
795223646324
785037759224

UPS:
K2479825491         (UPS ground)
J4603636537         (UPS next day express)
1Z87585E4391018698  (regular)

Patterns I am using (php code). Yep I gave up and started testing against all the patterns at my disposal. Had to write the second UPS one.

public function getCarrier($trackingNumber){



    $matchUPS1      = '/\b(1Z ?[0-9A-Z]{3} ?[0-9A-Z]{3} ?[0-9A-Z]{2} ?[0-9A-Z]{4} ?[0-9A-Z]{3} ?[0-9A-Z]|[\dT]\d\d\d ?\d\d\d\d ?\d\d\d)\b/';
    $matchUPS2      = '/^[kKJj]{1}[0-9]{10}$/';

    $matchUSPS0     = '/(\b\d{30}\b)|(\b91\d+\b)|(\b\d{20}\b)/';
    $matchUSPS1     = '/(\b\d{30}\b)|(\b91\d+\b)|(\b\d{20}\b)|(\b\d{26}\b)| ^E\D{1}\d{9}\D{2}$|^9\d{15,21}$| ^91[0-9]+$| ^[A-Za-z]{2}[0-9]+US$/i';
    $matchUSPS2     = '/^E\D{1}\d{9}\D{2}$|^9\d{15,21}$/';
    $matchUSPS3     = '/^91[0-9]+$/';
    $matchUSPS4     = '/^[A-Za-z]{2}[0-9]+US$/';
    $matchUSPS5     = '/(\b\d{30}\b)|(\b91\d+\b)|(\b\d{20}\b)|(\b\d{26}\b)| ^E\D{1}\d{9}\D{2}$|^9\d{15,21}$| ^91[0-9]+$| ^[A-Za-z]{2}[0-9]+US$/i';

    $matchFedex1    = '/(\b96\d{20}\b)|(\b\d{15}\b)|(\b\d{12}\b)/';
    $matchFedex2    = '/\b((98\d\d\d\d\d?\d\d\d\d|98\d\d) ?\d\d\d\d ?\d\d\d\d( ?\d\d\d)?)\b/';
    $matchFedex3    = '/^[0-9]{15}$/';


    if(preg_match($matchUPS1, $trackingNumber) || 
        preg_match($matchUPS2, $trackingNumber))
     {
        echo('UPS');

        $carrier = 'UPS';
        return $carrier;
    } else if(preg_match($matchUSPS0, $trackingNumber) || 
              preg_match($matchUSPS1, $trackingNumber) ||
              preg_match($matchUSPS2, $trackingNumber) ||
              preg_match($matchUSPS3, $trackingNumber) ||
              preg_match($matchUSPS4, $trackingNumber) ||
              preg_match($matchUSPS5, $trackingNumber)) {

        $carrier = 'USPS';
        return $carrier;
    } else if (preg_match($matchFedex1, $trackingNumber) || 
               preg_match($matchFedex2, $trackingNumber) || 
               preg_match($matchFedex3, $trackingNumber)) {

        $carrier = 'FedEx';
        return $carrier;
    } else if (0){
        $carrier = 'DHL';
        return $carrier;
    } 



    return;

}



回答5:


You can try these (not guaranteed):

UPS:

\b(1Z ?[0-9A-Z]{3} ?[0-9A-Z]{3} ?[0-9A-Z]{2} ?[0-9A-Z]{4} ?[0-9A-Z]{3} ?[0-9A-Z]|[\dT]\d\d\d ?\d\d\d\d ?\d\d\d)\b

UPS:

\b(1Z ?\d\d\d ?\d\w\w ?\d\d ?\d\d\d\d ?\d\d\d ?\d|[\dT]\d\d\d ?\d\d\d\d ?\d\d\d)\b

USPost:

\b(\d\d\d\d ?\d\d\d\d ?\d\d\d\d ?\d\d\d\d ?\d\d\d\d ?\d\d|\d\d\d\d ?\d\d\d\d ?\d\d\d\d ?\d\d\d\d ?\d\d\d\d)\b

But please test before you use them. I recommend RegexBuddy.




回答6:


Been researching this for a while, and made these based mostly on the answers here.
These should cover everything, without being too lenient.
UPS:
/^(1Z\s?[0-9A-Z]{3}\s?[0-9A-Z]{3}\s?[0-9A-Z]{2}\s?[0-9A-Z]{4}\s?[0-9A-Z]{3}\s?[0-9A-Z]$|[\dT]\d{3}\s?\d{4}s?\d{3})$/i

USPS:
/^(EA|EC|CP|RA)\d{9}(\D{2})?$|^(7\d|03|23|91)\d{2}\s?\d{4}\s?\d{4}\s?\d{4}\s?\d{4}(\s\d{2})?$|^82\s?\d{3}\s?\d{3}\s?\d{2}$/i

FEDEX:
/^(((96|98)\d{5}\s?\d{4}$|^(96|98)\d{2})\s?\d{4}\s?\d{4}(\s?\d{3})?)$/i




回答7:


I use these in an eBay application I wrote:

USPS Domestic:

/^91[0-9]+$/

USPS International:

/^[A-Za-z]{2}[0-9]+US$/

FedEx:

/^[0-9]{15}$/

However, this might be eBay/Paypal specific, as all USPS Domestic labels start with "91". All USPS International labels start with two characters and end with "US". As far as I know, FedEx just uses 15 random digits.

(Please note that these regular expressions assume all spaces are removed. It would be fairly easy to allow for spaces though)




回答8:


In the event it helps anyone else searching for this, here is a simple Python class that resolves common formats: Gist #541851




回答9:


Check out this github project that lists a lot of PHP tracking regexes. https://github.com/darkain/php-tracking-urls




回答10:


Here are the ones I am now using in my Java app. These are determined by my experience of sucking tracking numbers out of shipping confirmation emails from a whole pile of drop ship services. I just made a new USPS one from scratch since none of the ones I found worked for some of my numbers based on example numbers on the USPS site. These only work for US tracking codes because we only sell in the US.

private final Pattern UPS_TRACKING_NUMBER =
    Pattern.compile("[^A-Za-z0-9](1Z[A-Za-z0-9]{6,})",
        Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

private final Pattern FEDEX_TRACKING_NUMBER =
    Pattern.compile("\\b((96|98)\\d{18,20}|\\d{15}|\\d{12})\\b",
        Pattern.MULTILINE);

private final Pattern USPS_TRACKING_NUMBER =
    Pattern.compile("\\b(9[2-4]\\d{20}(?:(?:EA|RA)\\d{9}US)?|(?:03|23|14|70)\\d{18})\\b",
        Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); 



回答11:


I'm working in an Angular2+ app and just put together a component to handle common US tracking numbers. It tests them using standard JavaScript RegExp's that I put together from this resource HERE & HERE and sets the href on an anchor tag with the tracking link URL if it's good. You don't have to be using Angular or TypeScript to easily adapt this to your application. I tested it out with different dummy numbers and seem to work dynamically so far. Please note, you can also switch out the null in the last else statement with the in-line commented url and it will send you to a Google search.

Any feedback (or if your tracking numbers don't work) please let me know I will update the answer. Thanks!

USAGE IN HTML:

<app-tracking-number [trackNum]="myTrackingNumberInput"></app-tracking-number>

COMPONENT .TS

import { Component, OnInit, Input } from '@angular/core';

@Component({
  selector: 'app-tracking-number',
  templateUrl: './tracking-number.component.html',
  styleUrls: ['./tracking-number.component.scss']
})
export class TrackingNumberComponent implements OnInit {
    @Input() trackNum:string;
    trackNumHref:string = null;
    // Carrier tracking numbers patterns from https://www.iship.com/trackit/info.aspx?info=24 AND https://www.canadapost.ca/web/en/kb/details.page?article=how_to_track_a_packa&cattype=kb&cat=receiving&subcat=tracking
    isUPS:RegExp = new RegExp('^1Z[A-H,J-N,P,R-Z,0-9]{16}$');               // UPS tracking numbers usually begin with "1Z", contain 18 characters, and do not contain the letters "O", "I", or "Q".
    isFedEx:RegExp = new RegExp('^[0-9]{12}$|^[0-9]{15}$');                 // FedEx Express tracking numbers are normally 12 digits long and do not contain letters AND FedEx Ground tracking numbers are normally 15 digits long and do not contain letters.
    isUSPS:RegExp = new RegExp('^[0-9]{20,22}$|^[A-Z]{2}[0-9,A-Z]{9}US$');  // USPS Tracking numbers are normally 20-22 digits long and do not contain letters AND USPS Express Mail tracking numbers are normally 13 characters long, begin with two letters, and end with "US".
    isDHL:RegExp = new RegExp('^[0-9]{10,11}$');                            // DHL tracking numbers are normally 10 or 11 digits long and do not contain letters.
    isCAPost:RegExp = new RegExp('^[0-9]{16}$|^[A-Z]{2}[0-9]{9}[A-Z]{2}$'); // 16 numeric digits (0000 0000 0000 0000) AND 13 numeric and alphabetic characters (AA 000 000 000 AA).

    constructor() { }

    ngOnInit() {
        this.setHref();
    }

    setHref() {
        if(!this.trackNum) this.trackNumHref = null;
        else if(this.isUPS.test(this.trackNum)) this.trackNumHref = `https://wwwapps.ups.com/WebTracking/processInputRequest?AgreeToTermsAndConditions=yes&loc=en_US&tracknum=${this.trackNum}&Requester=trkinppg`;
        else if(this.isFedEx.test(this.trackNum)) this.trackNumHref = `https://www.fedex.com/apps/fedextrack/index.html?tracknumber=${this.trackNum}`;
        else if(this.isUSPS.test(this.trackNum)) this.trackNumHref = `https://tools.usps.com/go/TrackConfirmAction?tLabels=${this.trackNum}`;
        else if(this.isDHL.test(this.trackNum)) this.trackNumHref = `http://www.dhl.com/en/express/tracking.html?AWB=${this.trackNum}&brand=DHL`;
        else if(this.isCAPost.test(this.trackNum)) this.trackNumHref =`https://www.canadapost.ca/trackweb/en#/search?searchFor=${this.trackNum}`;
        else this.trackNumHref = null; // Google search as fallback... `https://www.google.com/search?q=${this.trackNum}`;
    }

}

COMPONENT .HTML

<a *ngIf="trackNumHref" [href]="trackNumHref" target="_blank">{{trackNum}}</a>
<span *ngIf="!trackNumHref">{{trackNum}}</span>



回答12:


I believe FedEx is 12 digits:

^[0-9]{12}$



回答13:


I also came across tracking numbers from FedEx with 22 digits recently, so watch out! I haven't found any good reference for the FedEx's general format yet.

FedEx Example #: 9612019059803563050071




回答14:


Here's an up to date regex for UPS (works with regular and Mail Innovation type tracking numbers):

\b(1Z ?[0-9A-Z]{3} ?[0-9A-Z]{3} ?[0-9A-Z]{2} ?[0-9A-Z]{4} ?[0-9A-Z]{3} ?[0-9A-Z]|[\dT]\d\d\d ?\d\d\d\d ?\d\d\d|\d\d\d ?\d\d\d ?\d\d\d|\d{22,34})\b




回答15:


Late to the party however, the below will work with 26 char USPS numbers as well.

/(\b\d{30}\b)|(\b91\d+\b)|(\b\d{20}\b)|(\b\d{26}\b)|^E\D{1}\d{9}\D{2}$|^9\d{15,21}$|^91[0-9]+$|^[A-Za-z]{2}[0-9]+US$/i



回答16:


I know there are already lots of answers and that this was asked a long time ago, but I don't see a single one that addresses all the possible USPS tracking numbers with a single expression.

Here is what I came up with:

((\d{4})(\s?\d{4}){4}\s?\d{2})|((\d{2})(\s?\d{3}){2}\s?\d{2})|((\D{2})(\s?\d{3}){3}\s?\D{2})

See it working here: http://regexr.com/3e61u



来源:https://stackoverflow.com/questions/619977/regular-expression-patterns-for-tracking-numbers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!