【发布时间】:2020-12-21 09:34:47
【问题描述】:
我有几行:
[3G*2110620000*000A*UD]
[3G*2110620000*000A*LK][3G*2110620000*000A*LK,0,0,100]
[3G*2110620000*000A*LK,100]
[EL*2110620006*000A*LK,0,0],100]]]
结果我需要得到一个数组:
Array(
[0] => Array
(
[Manufacture] => 3G
[DeviceId] => 2110620000
[Length] => 000A
[Command] => UD
),
[1] => Array
(
[Manufacture] => 3G
[DeviceId] => 2110620000
[Length] => 000A
[Command] => LK
),
[2] => Array
(
[Manufacture] => 3G
[DeviceId] => 2110620000
[Length] => 000A
[Command] => LK
[Parameters] => 0,0,100
),
[3] => Array
(
[Manufacture] => 3G
[DeviceId] => 2110620000
[Length] => 000A
[Command] => LK
[Parameters] => 100
),
[4] => Array
(
[Manufacture] => 3G
[DeviceId] => 2110620000
[Length] => 000A
[Command] => LK
[Parameters] => 0,0],100]]
),
)
每一行可以包含一个或多个命令。
- 命令以“[”开头,以“]”结尾。
- 命令必须在“[”之后包含制造商名称“3G”或“EL”。
- 接下来是“*”分隔符。
- 接下来是一个 10 位数字是设备标识符。
- 接下来是一个 4 字节的数据包长度。
- 接下来是命令名称(可以是任意长度)。
- 接下来是“,”分隔符。
- 接下来是命令参数(它们可能不存在,它们可以包含任何字符,包括“]”和“[”)。
我正在尝试为这些情况编写一个正则表达式,但我无法突出显示参数。
我试过了
\[(?P<Manufacture>3G|EL)\*(?P<DeviceId>[0-9]{10})\*(?P<Length>[A-Z0-9]{4})\*(?P<Command>.+)[\]|,]{1}(?P<Parameters>.*)\]?
【问题讨论】:
标签: php regex preg-match-all