1

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?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Fr3ak1n0ut
  • 71
  • 9
  • 2
    Yes, it is theoretically possible, but I wouldn't do it like that. First because we're talking about bytes, which is not really text and will thus be complex to match using regex which is text oriented (with possible locale support). The UDS protocol you have to parse, will be most probably easier to parse using a regular parser, or even using already existing libraries. – Cilyan Nov 11 '15 at 13:50
  • So you want one regex for all VIN formats? – pacholik Nov 11 '15 at 14:18
  • Dont get me wrong - I already managed to retrieve the data - I am just generally interested if it would be possible retrieving it via regex. Thanks @Cilyan for pointing that out. – Fr3ak1n0ut Nov 12 '15 at 14:32
  • @pacholik yep, pretty much – Fr3ak1n0ut Nov 12 '15 at 14:42

1 Answers1

-2

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'
  • 1
    this question is in python. – d0nut Nov 11 '15 at 14:40
  • OP said *Where xx are the bytes containing the data*. The data is not literal `xx`, those are just placeholders for the hex representation of the data. – Josh J Nov 11 '15 at 14:42
  • Got it. I wasn't sure if he had the bytes converted to string. And I blame the lack of coffee for missing the python part. – Leo Meirelles Nov 11 '15 at 16:26