请教一个关于re.compile多重匹配的问题

import re
s = ‘Condiction=BTFreqSweep tech=BT;subtc=Power stage=After_Cal;ant=1;rate=4-DH1;cc=2;pCap=4;freq=2402’
p = re.compile(r”Condiction=(\w+)ant=(\d+)rate=(\w)freq=(\d+)”)
priint(p.findall(s))

我想匹配出下面的格式应该怎么写,求帮助, 谢谢 !!!
[‘BTFreqSweep’, ‘1’, ‘4-DH1’, ‘2402’]

Jason990420
最佳答案

Try this

import re

s = 'Condiction=BTFreqSweep tech=BT;subtc=Power stage=After_Cal;ant=1;rate=4-DH1;cc=2;pCap=4;freq=2402'
p = re.compile(r'Condiction=(\w+).+;ant=(\d+);rate=(.+?);.+;freq=(\d+)')
for matches in p.findall(s):                # ['BTFreqSweep', '1', '4-DH1', '2402']
    print(list(matches))

p = re.compile(r'(?:Condiction|ant|rate|freq)=(.+?)(?:[\s;]|\Z)')
print([match for match in p.findall(s)])    # ['BTFreqSweep', '1', '4-DH1', '2402']
1年前 评论
讨论数量: 2
Jason990420

Try this

import re

s = 'Condiction=BTFreqSweep tech=BT;subtc=Power stage=After_Cal;ant=1;rate=4-DH1;cc=2;pCap=4;freq=2402'
p = re.compile(r'Condiction=(\w+).+;ant=(\d+);rate=(.+?);.+;freq=(\d+)')
for matches in p.findall(s):                # ['BTFreqSweep', '1', '4-DH1', '2402']
    print(list(matches))

p = re.compile(r'(?:Condiction|ant|rate|freq)=(.+?)(?:[\s;]|\Z)')
print([match for match in p.findall(s)])    # ['BTFreqSweep', '1', '4-DH1', '2402']
1年前 评论

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