I have the following 3 strings containing binary data.
s1="\t 28890\tABGT\tXYZW\t 94 23 08 92 00 00 00 EC 02 10 00 E2 00 4B\t\x00\x00\x00\x00\x01\f".force_encoding("ASCII-8BIT")
s2=" \t0000013\t123\t9886\t 95 83 49 26 0E 82 00 A6 08 02 06 C0\x00\x00\x00\x00\x02\xB2\x00\x00\x00\x00\b\xFEF".force_encoding("ASCII-8BIT")
s3=" \t0000013\t123\t9HN3\t 95 83 49 26 0E 82 00 A6 08 02 06 C0\xA1\x02\x00\x00\x02\xB2\b\xFEF".force_encoding("ASCII-8BIT")
I have the following 3 similar regex to get the bytes between *\t and something beginning with \ (i.e. \t, \x00, \xB2, \xFEF)
s1[/(?<=[A-Z]{4}\t ).+?(?=\t)/]
s2[/(?<=[0-9]{4}\t ).+?(?=\x00)/]
s3[/(?<=.+\t ).+?(?=\x..)/]
The first 2 regex work for string s1 and s2 but how could be a more general regex to match the 3 cases?
I tried the regex s3[/(?<=.+\t ).+?(?=\x..)/] but I get error below.
irb(main):> s1[/(?<=[A-Z]{4}\t ).+?(?=\t)/]
=> "94 23 08 92 00 00 00 EC 02 10 00 E2 00 4B"
irb(main):> s2[/(?<=[0-9]{4}\t ).+?(?=\x00)/]
=> "95 83 49 26 0E 82 00 A6 08 02 06 C0"
irb(main):> s3[/(?<=.+\t ).+?(?=\x..)/]
SyntaxError: (irb):4953: invalid hex escape
s3[/(?<=.+\t ).+?(?=\x..)/]
^
invalid pattern in look-behind: /(?<=.+\t ).+?(?=..)/
from /usr/bin/irb:11:in `<main>'
I think I only need to have the correct regex or is there a better way to extract the desired values without using regex?
Thanks for any help
(?<=.+\t )is invalid because Ruby's lookbehinds cannot be variable length.