سؤال

I'm trying to use a RegEx that will catch any pre 1980's VIN (Vehicle Identification Numbers). Anythign pre 1980's will not be required to be 17 digits and can contain 'O's and 'U's.

This site claimed to have a RegEx for it (it also explains the VIN rules): http://lamptricks.blogspot.com/2012/03/vin-regex-pre-1980-and-new.html

Here's the RegEx: ^((([a-h,A-H,j-n,J-N,p-z,P-Z,0-9]{9})([a-h,A-H,j-n,J-N,p,P,r-t,R-T,v-z,V-Z,0-9])([a-h,A-H,j-n,J-N,p-z,P-Z,0-9])(\d{6}))|(([a-h,A-H,j-z,J-Z,0-9]{6,11})(\d{5})))$

But the following VIN did not pass the test: BCG23253

It ends and 5 digits and is 8 characters long-- which thie RegEx accounts for... Is this VIN just faulty or is the RegEx?

هل كانت مفيدة؟

المحلول

First of all, the regex you found needs some work. I think the author doesn't understand what commas mean inside character classes, for one thing. If you ignore the needless commas and the capture groups, you can simplify the whole thing to this:

/^([a-hj-mp-z0-9]{9}[a-hj-mp-rtv-z0-9][a-hj-mp-z0-9]\d{6}|[a-hj-z0-9]{6,11}\d{5})$/i

...and then further, depending on your regex engine:

/^((?!.{9}[su])[a-z0-9-[io]]{11}\d{6}|[a-hj-z0-9]{6,11}\d{5})$/i

That being said, the number you gave (BCG23253) doesn't satisfy the requirement because it's only 8 characters long. To satisfy the bare minimum requirements (the [a-hj-z0-9]{6-11}\d{5} part above), your input would have to be 11-17 characters long, end in 5 numbers, and not include the letter I.

So BCG23253 shouldn't pass, but BCGBCG23253 (for example) would. As I said in my comment above, I think we need more information about the specific matches you're looking for. It sounds to me like the regex you've posted is made for matching post-1980 VINs, not pre-1980 VINs. Either that, or BCG23253 isn't a valid VIN after all.

نصائح أخرى

That RegEx is hard to read, but look at this, after the or operator:

(([a-h,A-H,j-z,J-Z,0-9]{6,11})(\d{5})))$

...says "between 6 and 11 of the preceding group, followed by 5 digits". Your sample VIN does not meet that criteria.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top