关于正则多个匹配的问题

我有个下面的字符串,我想匹配其中的多个条件,比如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年前 评论
jwwlchen (楼主) 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年前 评论
jwwlchen (楼主) 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年前 评论

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