Jump to content

U.S. car VIN Regex


2levelsabove

Recommended Posts

the best I have been able to research so far is:

 

Regex: US vehicle VIN numbers for vehicles with GVW < 10k lbs

 

*********************************************

 

^([A-Z]{3}|\d{3})[A-Z]{2}\d{2}([A-Z]{1}|\d{1})([\d{1}|X{1})([A-Z]+\d+|\d+[A-Z]+)\d{5}$

 

 

any better out there ? I know this is a touch one.

 

Link to comment
Share on other sites

Perhaps showing some sample VINs for those of us not familiar with the length / format. I was looking at your pattern to try and see what you came up with, but towards the end, you have [A-Z]+ (so this doesn't tell me how many characters to look for.. nor does \d+) Be clear and show some valid examples so that others can verify what you came up with and see what can be done differently.

 

Cheers,

 

NRG

Link to comment
Share on other sites

Ok, here's my take on it: I used this hypothetical VIN: 368TU79MXH4763452

I think it is valid by what you are saying. So to recap with regards to the format:

 

  • Must be 17 characters.
  • Cannot contain I, O or Q (this is reflected in my regex as: [A-HJ-NPR-Z]).

 

$str='368TU79MXH4763452';
if(strlen($str) != 17){
   echo 'Invalide VIN length!';
} else {
   if (preg_match('#^([A-HJ-NPR-Z]{3}|\d{3})[A-HJ-NPR-Z]{2}\d{2}([A-HJ-NPR-Z]|\d)(\d|X)([A-HJ-NPR-Z]+\d+|\d+[A-HJ-NPR-Z]+)\d{6}$#', $str, $match)){
      echo 'Valid VIN found!';
   } else {
      echo 'Invalid VIN!';
   }
}

 

In my tests, this seems to work (of course, if I am misunderstanding the VIN format, then I'm off base).

Now my pattern is probably not the best way to go about this.. it is my off the top of my head brute attempt. Let me know if this does indeed work as intended.

 

Cheers,

 

NRG

 

EDIT: I'm going to try and see if I can reduce the length of this regex by using additional parenthesis / back referencing. Will post shortly.

Link to comment
Share on other sites

This pattern seems to hold up as well:

 

 

(preg_match('#^(?[A-HJ-NPR-Z]){3}|\d{3})(?1){2}\d{2}(??1)|\d)(?:\d|X)(??1)+\d+|\d+(?1)+)\d{6}$#', $str, $match)

 

The idea here is that since we have a sub pattern [A-HJ-NPR-Z] used multiple times, I captured the initial one as $1, then use (?1) as a recursion.

Let me know if you find anything that I didn't quite get right.

 

Cheers,

 

NRG

 

EDIT: I also added some 'do not capture (?: ) parenthesis across the alternation aspects (which I should have done in the first place)...

 

 

Link to comment
Share on other sites

VIN should have only A-Z, 0-9 characters, but not I, O, or Q

Last 6 characters of VIN should be a number

VIN should be 17 characters long

 

/\A[A-HJ-NPR-Z\d]{11}\d{6}\z/

 

I may be misunderstanding something here.. going by the OP's intial stab at a pattern for VINs, it is:

^([A-Z]{3}|\d{3})[A-Z]{2}\d{2}([A-Z]{1}|\d{1})([\d{1}|X{1})([A-Z]+\d+|\d+[A-Z]+)\d{5}$

This tells me the format is basically:

 

either 3 letters or 3 digit, then...

either 2 letters and 2 digit, then...

either a single letter or single digit, then...

a single digit or X, then..

a possible mix of letters and digits, with the last 5 being digits (which is itself contradictory to the last 6 needing to be digits, as the 6th last one according to the original pattern can be a digit or a letter.. but that's not what I'm getting at).

 

Effigy, according to your pattern, there could possible be alternate letter / digits..all the way through the first 11 characters... I am going by either 3 letters or 3 digits (as the first three characters for example). Am I misunderstanding the format of the VIN?

 

Link to comment
Share on other sites

I tried both :

$pattern1="/\A[A-HJ-NPR-Z\d]{11}\d{6}\z/";

$pattern2="([A-Z]{3}|\d{3})[A-Z]{2}\d{2}([A-Z]{1}|\d{1})([\d{1}|X{1})([A-Z]+\d+|\d+[A-Z]+)\d{5}";

 

 

pattern 1 was not able to find "1FMYU24E1WUD17422 " in a block of text.

 

 

 

and pattern2 returned an error:    Warning: preg_match_all() [function.preg-match-all]: Unknown modifier '['

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.