我想用正则表达式解析“Intel HEX”文件(有关文件格式规范,请参阅wikipedia)。
Record structure
A record (line of text) consists of six fields (parts) that appear in order from left to right:
Start code, one character, an ASCII colon ':'.
Byte count, two hex digits, indicating the number of bytes (hex digit pairs) in the data field. The maximum byte count is 255 (0xFF). 16 (0x10) and 32 (0x20) are commonly used byte counts.
Address, four hex digits, representing the 16-bit beginning memory address offset of the data. The physical address of the data is computed by adding this offset to a previously established base address, thus allowing memory addressing beyond the 64 kilobyte limit of 16-bit addresses. The base address, which defaults to zero, can be changed by various types of records. Base addresses and address offsets are always expressed as big endian values.
Record type (see record types below), two hex digits, 00 to 05, defining the meaning of the data field.
Data, a sequence of n bytes of data, represented by 2n hex digits. Some records omit this field (n equals zero). The meaning and interpretation of data bytes depends on the application.
Checksum, two hex digits, a computed value that can be used to verify the record has no errors.
示例行是
:10010000214601360121470136007EFE09D2190140
我可以使用此正则表达式轻松捕获前3个:
^:(?<ByteCount>[0-9a-fA-F]{2})(?<Address>[0-9a-fA-F]{4})(?<RecordType>0[0-5])
但数据长度为[0-9a-fA-F]{X}
,其中X是命名捕获组ByteCount的值。那可能吗?例如(?<Data>[0-9a-fA-F]{\k<ByteCount>})