关于正则多个匹配的问题 | python | python 技术论坛-江南app体育官方入口

我有个下面的字符串,我想匹配其中的多个条件,比如npcondition,freq ,ant ,pwr

tc=txpower:npcondition=ao:subsubtc=avg tech=wlan:subtc=avg:freq=2442:ant=1:subtech=ax20mhz:rate=mcs9:pwr=-10:stream=1

但是我只会一个一个的匹配,怎么用一个语句匹配出这4个条件?

jason990420
最佳答案
import re
text = "tc=txpower:npcondition=ao:subsubtc=avg tech=wlan:subtc=avg:freq=2442:ant=1:subtech=ax20mhz:rate=mcs9:pwr=-10:stream=1"
print([match.split('=') for match in re.split(':| ', text)])
print()
print([match for match in re.findall('(\w )=(-?\w )', text)])
[['tc', 'txpower'], ['npcondition', 'ao'], ['subsubtc', 'avg'], ['tech', 'wlan'], ['subtc', 'avg'], ['freq', '2442'], ['ant', '1'], ['subtech', 'ax20mhz'], ['rate', 'mcs9'], ['pwr', '-10'], ['stream', '1']]
[('tc', 'txpower'), ('npcondition', 'ao'), ('subsubtc', 'avg'), ('tech', 'wlan'), ('subtc', 'avg'), ('freq', '2442'), ('ant', '1'), ('subtech', 'ax20mhz'), ('rate', 'mcs9'), ('pwr', '-10'), ('stream', '1')]
1年前
(楼主) 1年前
讨论数量: 3
jason990420
import re
text = "tc=txpower:npcondition=ao:subsubtc=avg tech=wlan:subtc=avg:freq=2442:ant=1:subtech=ax20mhz:rate=mcs9:pwr=-10:stream=1"
print([match.split('=') for match in re.split(':| ', text)])
print()
print([match for match in re.findall('(\w )=(-?\w )', text)])
[['tc', 'txpower'], ['npcondition', 'ao'], ['subsubtc', 'avg'], ['tech', 'wlan'], ['subtc', 'avg'], ['freq', '2442'], ['ant', '1'], ['subtech', 'ax20mhz'], ['rate', 'mcs9'], ['pwr', '-10'], ['stream', '1']]
[('tc', 'txpower'), ('npcondition', 'ao'), ('subsubtc', 'avg'), ('tech', 'wlan'), ('subtc', 'avg'), ('freq', '2442'), ('ant', '1'), ('subtech', 'ax20mhz'), ('rate', 'mcs9'), ('pwr', '-10'), ('stream', '1')]
1年前
(楼主) 1年前
>>> re.findall(r'(\w )=(-?\w )', text)
[('tc', 'txpower'), ('npcondition', 'ao'), ('subsubtc', 'avg'), ('tech', 'wlan'), ('subtc', 'avg'), ('freq', '2442'), ('ant', '1'), ('subtech', 'ax20mhz'), ('rate', 'mcs9'), ('pwr', '-10'), ('stream', '1')]
1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
网站地图