【问题标题】:python regex in string commande字符串命令中的python正则表达式
【发布时间】: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


【解决方案1】:

通过使用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

【讨论】:

  • re.findall(r':(.*)', A) 会给你同样的结果。
  • @TheIdealis;我只是展示了一种不使用正则表达式和字符串方法的方法。
  • 嗨,@Rakesh 谢谢你这是一个很好的 wya 但问题是 bssid 是六进制的所以它是这样的:ec:9b:f3:b4:bb:09 所以唯一的在被丢弃之前检索到的所有内容都是 09 ,但似乎正在使用正则表达式 (r':(.*)', A) 。我会继续尝试,看看我是否得到了正确的顺序。
  • @AitZaid;这可以通过更改拆分方法来解决。立即更新解决方案。
  • @AitZaid;将 i.split(" : ") 更改为 i.split(" : ") 可以解决问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-10
  • 2012-05-28
  • 1970-01-01
  • 1970-01-01
  • 2012-02-17
  • 2020-11-29
  • 2020-05-05
相关资源
最近更新 更多