2levelsabove Posted September 17, 2008 Share Posted September 17, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/124660-us-car-vin-regex/ Share on other sites More sharing options...
nrg_alpha Posted September 17, 2008 Share Posted September 17, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/124660-us-car-vin-regex/#findComment-643948 Share on other sites More sharing options...
2levelsabove Posted September 18, 2008 Author Share Posted September 18, 2008 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 ummm should make an interesting regex. oh hey and thanks for your help in the other threads Quote Link to comment https://forums.phpfreaks.com/topic/124660-us-car-vin-regex/#findComment-644324 Share on other sites More sharing options...
nrg_alpha Posted September 18, 2008 Share Posted September 18, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/124660-us-car-vin-regex/#findComment-644335 Share on other sites More sharing options...
nrg_alpha Posted September 18, 2008 Share Posted September 18, 2008 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)... Quote Link to comment https://forums.phpfreaks.com/topic/124660-us-car-vin-regex/#findComment-644341 Share on other sites More sharing options...
effigy Posted September 18, 2008 Share Posted September 18, 2008 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/ Quote Link to comment https://forums.phpfreaks.com/topic/124660-us-car-vin-regex/#findComment-644851 Share on other sites More sharing options...
nrg_alpha Posted September 18, 2008 Share Posted September 18, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/124660-us-car-vin-regex/#findComment-644896 Share on other sites More sharing options...
effigy Posted September 19, 2008 Share Posted September 19, 2008 I was going by the OP's second post, but based on their first I see that they are looking for more specifics. Quote Link to comment https://forums.phpfreaks.com/topic/124660-us-car-vin-regex/#findComment-645819 Share on other sites More sharing options...
2levelsabove Posted September 23, 2008 Author Share Posted September 23, 2008 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 '[' Quote Link to comment https://forums.phpfreaks.com/topic/124660-us-car-vin-regex/#findComment-648266 Share on other sites More sharing options...
DarkWater Posted September 23, 2008 Share Posted September 23, 2008 You didn't enclose the second pattern in / /. Try doing that, and it should work. Quote Link to comment https://forums.phpfreaks.com/topic/124660-us-car-vin-regex/#findComment-648272 Share on other sites More sharing options...
2levelsabove Posted September 23, 2008 Author Share Posted September 23, 2008 $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}$/"; preg_match_all($pattern2, $content, $matches) still cannot find vins for example: YV1RS592572633234 was skipped in a block of text Quote Link to comment https://forums.phpfreaks.com/topic/124660-us-car-vin-regex/#findComment-648300 Share on other sites More sharing options...
2levelsabove Posted September 24, 2008 Author Share Posted September 24, 2008 *bump* anyone ? Quote Link to comment https://forums.phpfreaks.com/topic/124660-us-car-vin-regex/#findComment-649741 Share on other sites More sharing options...
effigy Posted September 25, 2008 Share Posted September 25, 2008 What about the VIN format indicates that a vehicle is "GVW < 10k lbs"? Quote Link to comment https://forums.phpfreaks.com/topic/124660-us-car-vin-regex/#findComment-650710 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.