如何在注册表中进一步缩小搜索条件?

2024-05-29 10:38:45 发布

您现在位置:Python中文网/ 问答频道 /正文

文件/变量包含以下行。你知道吗

Interface wlan1-cabin-2  
ifindex 37  
wdev 0x300000003  
addr 06:53:1a:4e:07:02  
ssid SSID3  
type AP  
channel 6 (2437 MHz), width: 20 MHz, center1: 2437 MHz  

ssid_regex = re.compile("wdev +0x300000003((.*\n){2})", re.MULTILINE)

按预期返回wdev 0x300000003下面的2行。你知道吗

如何进一步修改表达式以获得ssidSSID3空格后面的值的匹配?你知道吗


Tags: 文件retypechannelwidthinterfaceapaddr
2条回答

将该行的匹配项添加到regexp。你知道吗

ssid_regex = re.compile("wdev +0x300000003\n(.*)\nssid\s+(.*)")

在结果中,组1将包含addr行,组2将包含SSID3。你知道吗

不需要re.MULTILINE,因为在regexp中从不使用^$。你知道吗

这是另一个解决方案。你知道吗

import re

interface_info = '''Interface wlan1-cabin-2  
ifindex 37  
wdev 0x300000003 
addr 06:53:1a:4e:07:02    
ssid SSID3  
type AP  
channel 6 (2437 MHz), width: 20 MHz, center1: 2437 MHz'''

mac_address = re.search(r'(addr)\s.+', interface_info)
ssid_match = re.search(r'(ssid)\s.+', interface_info)
interface_type = re.search(r'(type)\s.+',interface_info)
channel_info = re.search(r'(channel).+',interface_info)
split_channel_info = re.split(r'\,', channel_info.group(0))

print (str(mac_address.group(0)).lstrip('addr'))
print (str(ssid_match.group(0)).lstrip('ssid'))
print (str(interface_type.group(0)).lstrip('type'))

print(str(split_channel_info[0]).strip())
print(str(split_channel_info[1]).strip())
print(str(split_channel_info[2]).strip())

这是之前提出的另一个解决方案。你知道吗

import re

interface_info = '''Interface wlan1-cabin-2  
ifindex 37  
wdev 0x300000003  
addr 06:53:1a:4e:07:02 
ssid SSID3  
type AP  
channel 6 (2437 MHz), width: 20 MHz, center1: 2437 MHz'''

interface_regex = re.compile('wdev +0x300000003(.*)\naddr(.*)\nssid(.*)\ntype(.*)\nchannel(.*)')
interface_extract = re.search(interface_regex,interface_info)
interface_split = re.split(r'\n', interface_extract.group(0))

mac_address = str(interface_split[1]).strip('addr')
ssid = str(interface_split[2]).strip('ssid')
interface_type = str(interface_split[3]).strip('type')
channel_info = interface_split[4]
split_channel_info = re.split(r'\,', channel_info)

相关问题 更多 >

    热门问题