【发布时间】:2018-02-02 11:19:16
【问题描述】:
您好,我是 python 和正则表达式的新手。 我要完成的任务是从字符串中提取信息:这是代码
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 ....
我怎样才能以正确的顺序获得结果?
感谢您的帮助。
【问题讨论】:
-
我无法重现您的输出。它与您发布的代码不匹配;输入中的任何地方都没有“yournetwork”,但不知何故它在输出中。如果您想知道您的代码有什么问题,请发布minimal reproducible example。
-
原因不是你的正则表达式,它工作得很好。会不会是你第二次执行了
netsh- 命令? -
是的,正则表达式有效 :) 我让我第二次重新运行它
标签: python regex networking wifi