简体   繁体   English

从字符串中提取同一匹配项的多个实例

[英]Extract Multiple Instances of the Same Match from a String

I need to extract the value of all instances of pattern match of ssid in a string.我需要提取字符串中ssid模式匹配的所有实例的value

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 wlan1-cabin-1  
                ifindex 36  
                wdev 0x300000002  
                addr 06:53:1a:4e:07:01  
                ssid SSIDTEST2  
                type AP  
                channel 6 (2437 MHz), width: 20 MHz, center1: 2437 MHz  
        Interface wlan1  
                ifindex 7
                wdev 0x300000001  
                addr 06:53:1a:4e:07:00  
                ssid SSID1 
                type AP  
                channel 6 (2437 MHz), width: 20 MHz, center1: 2437 MHz  '''

ssid_regex = re.compile('ssid (\w+)')              
ssid_extract = re.search(ssid_regex,interface_info)

print (ssid_extract)

returns a value for first match only.仅返回第一次匹配的值。 I need to extract the value of every ssid match [SSID3, SSIDTEST2, SSID1}.我需要提取每个ssid匹配 [SSID3, SSIDTEST2, SSID1} 的值。 SSID3 SSID3

Actual SSID3实际 SSID3
expected [SSID3, SSIDTEST2, SSID1]预期 [SSID3、SSIDTEST2、SSID1]

!/usr/bin/env python !/usr/bin/env python

import re interface_info = '''导入重新 interface_info = '''
phy#3物理#3
Interface wlan1-cabin-1接口 wlan1-cabin-1
ifindex 36如果索引 36
wdev 0x300000002 wdev 0x300000002
addr 06:53:1a:4e:07:01地址 06:53:1a:4e:07:01
ssid SSIDTEST3 SSID SSIDTEST3
type AP类型AP
channel 6 (2437 MHz), width: 20 MHz, center1: 2437 MHz通道 6 (2437 MHz),宽度:20 MHz,中心 1:2437 MHz
Interface wlan1接口 wlan1
ifindex 7如果索引 7
wdev 0x300000001 wdev 0x300000001
addr 06:53:1a:4e:07:00地址 06:53:1a:4e:07:00
ssid SSIDTEST2 ssid SSIDTEST2
type AP类型AP
channel 6 (2437 MHz), width: 20 MHz, center1: 2437 MHz通道 6 (2437 MHz),宽度:20 MHz,中心 1:2437 MHz
phy#2物理#2
Interface wlan0接口 wlan0
ifindex 6如果索引 6
wdev 0x200000001 wdev 0x200000001
addr 02:ac:1a:4e:07:00地址 02:ac:1a:4e:07:00
ssid SSID1 SSID1
type AP类型AP
channel 149 (5745 MHz), width: 80 MHz, center1: 5775 MHz通道 149 (5745 MHz),宽度:80 MHz,中心 1:5775 MHz

''' interface_info = re.sub(re.compile('^\\s+', re.MULTILINE), '', interface_info) ssid_regex = re.compile('ssid (\\w+)') ssid_extract = re.findall(ssid_regex, interface_info[interface_info.index("Interface wlan1"):]) ''' interface_info = re.sub(re.compile('^\\s+', re.MULTILINE), '', interface_info) ssid_regex = re.compile('ssid (\\w+)') ssid_extract = re.findall(ssid_regex , interface_info[interface_info.index("Interface wlan1"):])

print (ssid_extract) print (type(ssid_extract))打印(ssid_extract)打印(类型(ssid_extract))

print(str(interface_info))打印(str(interface_info))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM