【发布时间】:2020-09-30 03:41:40
【问题描述】:
我有一个正则表达式模式,它部分地捕捉到了我想要的东西。该模式可以看起来像这些中的任何一个
"caller command"
"caller command specifier"
"caller command 'two-worded specifier'"
"caller 'two-worded command' specifier"
"caller 'two-worded command' 'two-worded specifier'"
我当前的代码将它们匹配到命名组中,并使用 Python 的 re 库文档中显示的是/否模式。
messages = ["your.majesty hello", "proclamation honor Dom", "your.majesty query 'Weekly Coding Challenge'", "your.majesty 'build test' submissions", "your.majesty 'build test' 'Weekly Coding Challenge'"]
call = "(?P<call>.*?)"
command = "(?P<command>'(.*?)'|(.*?))"
specifier = "(?P<specifier>'(.*?.)'|(.*?))"
duo = f"{call}\s{command}"
trio = f"({call}\s{command}\s{specifier})"
regex_duo = re.compile(duo, flags=re.DOTALL)
regex_trio = re.compile(trio)
for msg in messages:
match = regex_trio.match(msg)
if match is None:
match = regex_duo.match(msg)
print(match)
这个输出是
<re.Match object; span=(0, 13), match='your.majesty '>
<re.Match object; span=(0, 19), match='proclamation honor '>
<re.Match object; span=(0, 44), match="your.majesty query 'Weekly Coding Challenge'">
<re.Match object; span=(0, 26), match="your.majesty 'build test' ">
<re.Match object; span=(0, 51), match="your.majesty 'build test' 'Weekly Coding Challeng>
当我想要时
<re.Match object; span=(0, ...), match='your.majesty hello'>
<re.Match object; span=(0, ...), match='proclamation honor Dom'>
<re.Match object; span=(0, ...), match="your.majesty query 'Weekly Coding Challenge'">
<re.Match object; span=(0, ...), match="your.majesty 'build test' submissions">
<re.Match object; span=(0, ...), match="your.majesty 'build test' 'Weekly Coding Challenge'>
- 有没有比我目前正在做的更好的方法?
- 为什么即使我使用贪婪匹配,它也会截断这么多?
【问题讨论】:
-
鉴于您列表中的每个元素都在这里匹配,您要实现什么目标?
标签: python regex string string-matching