【发布时间】:2014-12-08 20:05:28
【问题描述】:
思考练习:编写一个接受正则表达式模式或字符串以完全匹配的 Python 函数的“最佳”方法是什么:
import re
strings = [...]
def do_search(matcher):
"""
Returns strings matching matcher, which can be either a string
(for exact match) or a compiled regular expression object
(for more complex matches).
"""
if not is_a_regex_pattern(matcher):
matcher = re.compile('%s$' % re.escape(matcher))
for s in strings:
if matcher.match(s):
yield s
那么,is_a_regex_pattern() 的实现思路呢?
【问题讨论】: