作者:羚瑞聪羊奶粉 | 来源:互联网 | 2023-05-17 09:03
Ihaveastring:我有一个字符串:StationDisconnect:1.3.6.1.4.1.11.2.14.11.15.2.75.3.2.0.8StaMAC:00:9F:
I have a string :
我有一个字符串:
Station Disconnect:1.3.6.1.4.1.11.2.14.11.15.2.75.3.2.0.8 StaMAC:00:9F:0B:00:38:B8 BSSID:00 9F Radioid:2
I want split this string. It look like this -
我想拆分这个字符串。它看起来像这样 -
'Station Disconnect:1.3.6.1.4.1.11.2.14.11.15.2.75.3.2.0.8' 'StaMAC:00:9F:0B:00:38:B8' 'BSSID:00 9F' 'Radioid:2'
I tried this logic - msgRegex = re.compile('[\w\s]+:') and split function also. How can I do Please help me Thank you
我尝试了这个逻辑 - msgRegex = re.compile('[\ w \ s] +:')和split函数。我该怎么办请帮帮我谢谢
2 个解决方案
1
From what I see, you have a problem when you have a whitespace inside the matches with hex values.
从我看到的,当你在匹配中有一个带有十六进制值的空格时,你会遇到问题。
Because of that, I believe you cannot use a splitting approach here. Match your tokens with a regex like
因此,我相信你不能在这里使用分裂方法。将您的令牌与正则表达式匹配
(?
See the regex demo
请参阅正则表达式演示
Python code:
import re
rx = r"(?
Result:
[('Station Disconnect', '1.3.6.1.4.1.11.2.14.11.15.2.75.3.2.0.8'), ('StaMAC', '00:9F:0B:00:38:B8'), ('BSSID', '00 9F'), ('Radioid', '2')]
[('Station Deassoc', '1.3.6.1.4.1.11.2.14.11.15.2.75.3.2.0.5'), ('StaMac1', '40:83:DE:34:04:75'), ('StaMac2', '40:83:DE:34:04:75'), ('UserName', '4083de340475'), ('StaMac3', '40:83:DE:34:04:75'), ('VLANId', '1'), ('Radioid', '2'), ('SSIDName', 'Devices'), ('SessionDuration', '12'), ('APID', 'CN58G6749V'), ('AP Name', '1023-noida-racking-zopnow'), ('BSSID', 'BC:EA:FA:DC:A6:F1')]
NOTE: If you need no tuples in the result, remove the capturing parentheses from the pattern.
注意:如果结果中不需要元组,请从模式中删除捕获括号。
Pattern details:
(? - start of string or whitespace followed with a word boundary (next char must be a letter/digit or _
)
(?
([^:]+)
- Capturing group #1: 1+ chars other than :
([^:] +) - 捕获组#1:1+以外的字符:
:
- a colon
: - 冒号
((?:[a-fA-F0-9]{2}(?:[ :][a-fA-F0-9]{2})*|\S)+)
- Capturing group 2 matching one or more occurrences of:
((?:[a-fA-F0-9] {2}(?:[:] [a-fA-F0-9] {2})* | \ S)+) - 捕获与一个或多个匹配的组2出现的次数:[a-fA-F0-9] {2}(?:[:] [a-fA-F0-9] {2})* - 2个十六进制字符后跟零或多次出现的空格或:和2个十六进制字符| - 或\ S - 非空白字符
\b
- trailing word boundary.
\ b - 尾随字边界。