简体   繁体   English

python中的正则表达式字典

[英]Regular expression dictionary in python

Is it possible to implement a dictionary with keys as regular expressions and actions (with parameters) as values? 是否可以使用键作为正则表达式和操作(带参数)作为值来实现字典?

for eg 例如

  1. key = "actionname 1 2", value = "method(1, 2)"
  2. key = "differentaction par1 par2", value = "appropriate_method(par1, par2)"

User types in the key, i need to execute the matching method with the parameters provided as part of user input. 密钥中的用户类型,我需要使用作为用户输入的一部分提供的参数来执行匹配方法。

It would be great if we can achieve the lookup in O(1) time, even if its not possible atleast i am looking for solutions to solve this problem. 如果我们能够在O(1)时间内完成查找,那将是很好的,即使它至少不可能,我正在寻找解决方案来解决这个问题。

I will be having few hundred regular expressions (say 300 ) and matching parameterized actions to execute. 我将有几百个正则表达式(比如300 )和匹配的参数化动作来执行。

I can write a loop to achieve this, but is there any elegant way to do this without using a for loop? 我可以写一个循环来实现这一点,但有没有优雅的方法来做到这一点,而不使用for循环?

Related question: Hashtable/dictionary/map lookup with regular expressions 相关问题: 使用正则表达式进行哈希表/字典/地图查找

Yes, it's perfectly possible: 是的,这是完全可能的:

import re
dict = {}
dict[re.compile('actionname (\d+) (\d+)')] = method
dict[re.compile('differentaction (\w+) (\w+)')] = appropriate_method

def execute_method_for(str):
    #Match each regex on the string
    matches = (
        (regex.match(str), f) for regex, f in dict.iteritems()
    )

    #Filter out empty matches, and extract groups
    matches = (
        (match.groups(), f) for match, f in matches if match is not None
    )


    #Apply all the functions
    for args, f in matches:
        f(*args)

Of course, the values of your dictionary can be python functions. 当然,你的字典的值可以是python函数。

Your matching function can try to match your string to each key and execute appropriate function if there is a match. 您的匹配函数可以尝试将字符串与每个键匹配,并在匹配时执行适当的函数。 This will be linear in time in the best case, but I don't think you can get anything better if you want to use regular expressions. 在最好的情况下,这将是线性的,但如果你想使用正则表达式,我认为你不能得到任何更好的东西。

But looking at your example data I think you should reconsider whether you need regular expressions at all. 但是看看你的示例数据,我认为你应该重新考虑是否需要正则表达式。 Perhaps you can just parse your input string into, eg <procedure-name> <parameter>+ and then lookup appropriate procedure by it's name (simple string), that can be O(1) 也许您可以将输入字符串解析为,例如<procedure-name> <parameter>+然后通过它的名称(简单字符串)查找适当的过程,可以是O(1)

Unfortunately this is not possible. 不幸的是,这是不可能的。 You will need to iterate over the regular expressions in order to find out if they match. 您需要迭代正则表达式以确定它们是否匹配。 The lookup in the dictionary will be O(1) though (but that doesn't solve your problem). 字典中的查找虽然是O(1) (但这并不能解决您的问题)。

IMHO, you are asking the WRONG QUESTION . 恕我直言,你问的是错误的问题

  1. You ask if there's an elegant way to do this. 你问是否有一种优雅的方式来做到这一点。 Answer: The most elegant way is the most OBVIOUS way. 答: 最优雅的方式是最明显的方式。 Code will be read 10x to 20x as often as it's modified. 代码将按照修改后的10倍到20倍读取。 Therefore, if you write something 'elegant' that's hard to read and quickly understand, you've just sabotaged the guy after you who has to modify it somehow. 因此,如果你写了一些难以阅读和快速理解的“优雅”,你就是在你不得不以某种方式修改它之后破坏了那个人。

  2. BETTER CODE: 更好的代码:

Another answer here reads like this: 这里的另一个答案如下:

matches = ( (regex.match(str), f) for regex, f in dict.iteritems() )

This is functionally equivalent (IMPORTANTLY, the same in terms of Python generated bytecode) to: 这在功能上是等效的(重要的是,在Python生成的字节码方面相同):

# IMHO 'regex' var should probably be named 'pattern' since it's type is <sre.SRE_Pattern>

for pattern, func in dictname.items():
    if pattern.match(str):
        func()

But, the below sample is hugely easier to read and understand at a glance . 但是,下面的示例一目了然,更容易阅读和理解

I apologize (a little) if you're one of those people who is offended by code that is even slightly more wordy than you think it could be. 如果你是那些被代码冒犯的人之一,那么我会道歉(一点点),这些代码甚至比你想象的还要冗长。 My criteria, and Guido's as mentioned in PEP-8, is that the clearest code is the best code. 我的标准和PEP-8中提到的Guido是最清晰的代码是最好的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM