-2

我有一个类似的字符串 8305473100342301010102BE7552773845527738451ACAF3D6C8743EC3 0000659A0000000500F107000104FFB50F0C,我需要将整个字符串解析为元素数组,但每个元素可能有不同的大小或长度。我想要的输出可能如下所示,

Array[0]=83;
Array[1]=05; 
Array[2]=4731003423; 
Array[3]=01; 
Array[4]=01; 
Array[5]=01; 
Array[6]=02; 
Array[7]=BE75; 
Array[8]=52773845; 
Array[9]=52773845; 
Array[10]=1ACAF3D6; 
Array[11]=C8743EC3; 
Array[12]=0000659A; 
Array[13]=00000005; 
Array[14]=00F1; 
Array[15]=07; 
Array[16]=00; 
Array[17]=0104; 
Array[18]=FFB5; 
Array[19]=0F; 
Array[20]=0C;
4

3 回答 3

1

用作\S+正则表达式。

  • \S匹配非空格字符。
  • X+匹配一个或多个x

这是 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')
于 2013-11-14T05:48:10.127 回答
0

Javascript 中的另一种方式,即 sans regex,是使用 Javascript split

"83 05 4731003423 01 01 01 02 BE75 52773845 52773845 1ACAF3D6 C8743EC3 0000659A 00000005 00F1 07 00 0104 FFB5 0F 0C".split(" ");
// returns ["83", "05", "4731003423", "01", "01", "01", "02", "BE75", "52773845", "52773845", "1ACAF3D6", "C8743EC3", "0000659A", "00000005", "00F1", "07", "00", "0104", "FFB5", "0F", "0C"]
于 2013-11-14T05:54:50.367 回答
0

string.split(' ');会给你那个确切的数组

于 2013-11-14T05:56:13.043 回答