本文介绍了通过一个正则表达式以任何顺序匹配多个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题中所述,我想要正则表达式,它将根据我的查询"按顺序给出结果.

as stated in heading I want regex which will give me results in order based on my 'query'.

line='VERSION="OTHER" POWER="LOW" FREQ="OFF" MAXTUN="BLER"'**

示例 1:

re.findall(r'FREQ="(.*?)"|VERSION="(.*?)"', line, re.MULTILINE)

输出为:

[('', 'OTHER'), ('OFF', '')]

我更喜欢的期望输出是这样的:

And desired output I prefer is something like:

['OFF', 'OTHER']

示例 2:

re.findall(r'VERSION="(.*?)"|FREQ="(.*?)"', line, re.MULTILINE)

输出是一样的:

[('', 'OTHER'), ('OFF', '')]

我更喜欢的期望输出是这样的:

And desired output I prefer is something like:

['OTHER', 'OFF']

有什么建议吗?

附言请不要问我想达到什么目的,并告诉我可能有更好的方法,除非您对此有疑问.

P.S.Please don't ask me what I want to achieve and tell me that there is maybe better way, only if you have some question regarding this.

谢谢!

推荐答案

您可以利用非捕获交替组来匹配 VERSIONFREQ(可选地以一个词边界,看看它是否符合你的要求):

You may leverage a non-capturing alternation group to match either VERSION or FREQ (optionally preceded with a word boundary, just check if it meets your requirements):

\b(?:VERSION|FREQ)="(.*?)"

查看正则表达式演示

详情

  • \b - 前导词边界
  • (?:VERSION|FREQ) - VERSIONFREQ
  • =" - =" 子字符串
  • (.*?) - 第 1 组(findall 的实际输出):除换行符以外的任何 0+ 个字符,尽可能少
  • " - 双引号.
  • \b - a leading word boundary
  • (?:VERSION|FREQ) - either VERSION or FREQ
  • =" - a =" substring
  • (.*?) - Group 1 (the actual output of findall): any 0+ chars other than line break chars, as few as possible
  • " - a double quote.

查看 Python 演示:

import re
line='VERSION="OTHER" POWER="LOW" FREQ="OFF" MAXTUN="BLER"'
print(re.findall(r'\b(?:VERSION|FREQ)="(.*?)"', line))
# => ['OTHER', 'OFF']

也许更好的主意是捕获键值对并将它们映射到字典:

A better idea, perhaps, is to capture key-value pairs and map them to a dictionary:

import re
line = 'VERSION="OTHER" POWER="LOW" FREQ="OFF" MAXTUN="BLER"'
results = re.findall(r'(VERSION|FREQ)="(.*?)"', line)
print(dict(results))
# => {'FREQ': 'OFF', 'VERSION': 'OTHER'}

请参阅 Python 演示.

这篇关于通过一个正则表达式以任何顺序匹配多个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-25 21:42