嗨,我是python和regex的新手。 我试图完成的任务是从字符串中提取信息:这是代码
import subprocess
import re
available = subprocess.check_output('netsh wlan show network mode=bssid',stderr=subprocess.STDOUT,universal_newlines=True,shell=True)
print(available)
输出是一个如下字符串:
Interface name : Wi-Fi 2
There are 15 networks currently visible.
SSID 1 : Rezalitchderk
Network type : Infrastructure
Authentication : WPA2-Personal
Encryption : CCMP
BSSID 1 : 62:f1:89:7c:71:d1
Signal : 91%
Radio type : 802.11n
Channel : 11
Basic rates (Mbps) : 1 2 5.5 11
Other rates (Mbps) : 6 9 12 18 24 36 48 54
SSID 2 : HUAWEI Mate 10 lite
Network type : Infrastructure
Authentication : WPA2-Personal
Encryption : CCMP
BSSID 1 : 1c:15:1f:3f:87:f9
Signal : 82%
Radio type : 802.11n
Channel : 11
Basic rates (Mbps) : 1 2 5.5 11
Other rates (Mbps) : 6 9 12 18 24 36 48 54
现在我想用正则表达式检索"之后的所有内容:"
所以我尝试了这个data= re.findall(r':(.*)', available)
但结果并不完全符合我的要求:result of the regex
正则表达式的结果应该是:wi-fi 2,Rezalchiderk,Infrastructure,Wpa2-Personal ....
如何以正确的顺序获得结果?
感谢您的帮助。
答案 0 :(得分:0)
使用 string.split 方法
A = '''Interface name : Wi-Fi 2
There are 15 networks currently visible.
SSID 1 : Rezalitchderk
Network type : Infrastructure
Authentication : WPA2-Personal
Encryption : CCMP
BSSID 1 : 62:f1:89:7c:71:d1
Signal : 91%
Radio type : 802.11n
Channel : 11
Basic rates (Mbps) : 1 2 5.5 11
Other rates (Mbps) : 6 9 12 18 24 36 48 54
SSID 2 : HUAWEI Mate 10 lite
Network type : Infrastructure
Authentication : WPA2-Personal
Encryption : CCMP
BSSID 1 : 1c:15:1f:3f:87:f9
Signal : 82%
Radio type : 802.11n
Channel : 11
Basic rates (Mbps) : 1 2 5.5 11
Other rates (Mbps) : 6 9 12 18 24 36 48 54'''
res = []
for i in A.split("\n"):
if ":" in i:
val = i.split(" : ")[-1]
print val.strip()
res.append(val.strip())
<强>输出:强>
Wi-Fi 2
Rezalitchderk
Infrastructure
WPA2-Personal
CCMP
d1
91%
802.11n
11
1 2 5.5 11
6 9 12 18 24 36 48 54
HUAWEI Mate 10 lite
Infrastructure
WPA2-Personal
CCMP
1c:15:1f:3f:87:f9
82%
802.11n
11
1 2 5.5 11
6 9 12 18 24 36 48 54