Type of compiled regex object in python

后端 未结 10 1091
一个人的身影
一个人的身影 2020-11-30 07:14

What is the type of the compiled regular expression in python?

In particular, I want to evaluate

isinstance(re.compile(\'\'), ???)

10条回答
  •  天涯浪人
    2020-11-30 07:43

    Prevention is better than cure. Don't create such a heterogeneous list in the first place. Have a set of allowed strings and a list of compiled regex objects. This should make your checking code look better and run faster:

    if input in allowed_strings:
        ignored = False
    else:
        for allowed in allowed_regexed_objects:
            if allowed.match(input):
                ignored = False
                break
    

    If you can't avoid the creation of such a list, see if you have the opportunity to examine it once and build the two replacement objects.

提交回复
热议问题