简体   繁体   中英

Regular Expression for specific VIN Pattern

I want to learn a bit about regular expressions. I have a String that represents the Vehicle Identification Number (VIN) in hexadecimal code.

The problem is, that the string will have a different pattern, depending on the car I am using to get it.

The first pattern is the following (spaces and newlines only for better visibility):

49 02 01 00 00 00 xx
49 02 02 xx xx xx xx
49 02 03 xx xx xx xx
49 02 04 xx xx xx xx
49 02 05 xx xx xx xx

Where xx are the bytes containing the data. As you can see, the data consists of 17 characters.

The second pattern that the car might return is the following (spaces and newlines only for better visibility):

014
0: 49 02 01 xx xx xx
1: xx xx xx xx xx xx xx
2: xx xx xx xx xx xx xx

Where xx are the bytes containing the data. As you can see, the data also consists of 17 characters.

I only want to read the xx part, and concatenate the result to a string that I will decode with str.decode("hex") to get my VIN.

So, my question is: is it possible to pack all this in a regular expression to get the final 17 bytes as result?

Please correct me if I missed something.

My understand is that your string is 490201000000xx490202xxxxxxxx490203xxxxxxxx490204xxxxxxxx490205xxxxxxxx and you want to extract the 'x' from it based on the position.

I wrote a small code that will handle in Javascript.

For the second case scenario, can you just remove the first 6 characters?

var a = '490201000000xx490202xxxxxxxx490203xxxxxxxx490204xxxxxxxx490205xxxxxxxx';
var reg = /.{12}(.{2}).{6}(.{8}).{6}(.{8}).{6}(.{8})/g;
var b = reg.exec(a);
var c = b.splice(1, 4);
var d = c.join(''); // d = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM