用作\S+
正则表达式。
这是 Javascript 示例:
> '83 05 4731003423 01 01 01 02 BE75 52773845 52773845 1ACAF3D6 C8743EC3 0000659A 00000005 00F1 07 00 0104 FFB5 0F 0C'.match(/\S+/g)
["83", "05", "4731003423", "01", "01", "01", "02", "BE75", "52773845", "52773845", "1ACAF3D6", "C8743EC3", "0000659A", "00000005", "00F1", "07", "00", "0104", "FFB5", "0F", "0C"]
> '83 AB 0C'.match(/\S+/g)
["83", "AB", "0C"]
> var dynamic_string = '83' + ' ' + 'AAAAAA' + ' ' + 'BEEF';
> dynamic_string.match(/\S+/g)
["83", "AAAAAA", "BEEF"]
更新
使用捕获组:
> var str = '83 05473100342301010102BE7552773845527738451ACAF3D6C8743EC3 0000659A0000000500F107000104FFB50F0C';
> str = str.replace(/\s+/g, '');
> var arr = str.match(/(.{2})(.{2})(.{8})(.{2})(.{2})(.{2})(.{2})(.{4})(.{8})(.{8})(.{8})(.{8})(.{8})(.{8})(.{4})(.{2})(.{2})(.{4})(.{4})(.{2})(.{2})(.{2})/);
"8305473100342301010102BE7552773845527738451ACAF3D6C8743EC3 0000659A0000000500F107000104FFB50F0C"
> arr
["8305473100342301010102BE7552773845527738451ACAF3D6C8743EC3 0000659A0000000500F107000104FFB50", "83", "05", "47310034", "23", "01", "01", "01", "02BE", "75527738", "45527738", "451ACAF3", "D6C8743E", "C3 00006", "59A00000", "0050", "0F", "10", "7000", "104F", "FB", "50", "0C"]
> arr.slice(1)
["83", "05", "47310034", "23", "01", "01", "01", "02BE", "75527738", "45527738", "451ACAF3", "D6C8743E", "C3 00006", "59A00000", "0050", "0F", "10", "7000", "104F", "FB", "50", "0C"]
顺便说一句,正则表达式似乎不是完成这项任务的最佳工具。
例如,在 Python 中,你可以在没有正则表达式的情况下做同样的事情:
>>> import struct
>>> s = '83 05473100342301010102BE7552773845527738451ACAF3D6C8743EC3 0000659A0000000500F107000104FFB50F0C'
>>> struct.unpack('2s2s8s2s2s2s2s4s8s8s8s8s8s8s4s2s2s4s4s2s2s2s2s', s)
('83', ' 0', '54731003', '42', '30', '10', '10', '102B', 'E7552773', '84552773', '8451ACAF', '3D6C8743', 'EC3 0000', '659A0000', '0005', '00', 'F1', '0700', '0104', 'FF', 'B5', '0F', '0C')