连接正则表达式对象的 Pythonic 方式 [英] Pythonic way to concatenate regex objects
问题描述
我有 python 正则表达式对象 - 比如 re_first 和 re_second - 我想连接.
I have python regex objects - say, re_first and re_second - I would like to concatenate.
import re
FLAGS_TO_USE = re.VERBOSE | re.IGNORECASE
re_first = re.compile( r"""abc #Some comments here """, FLAGS_TO_USE )
re_second = re.compile( r"""def #More comments here """, FLAGS_TO_USE )
我想要一个与上述任一正则表达式匹配的正则表达式.到目前为止,我已经
I want one regex expression that matches either one of the above regex expressions. So far, I have
pattern_combined = re_first.pattern + '|' + re_second.pattern
re_combined = re.compile( pattern_combined, FLAGS_TO_USE )
这不能很好地扩展更多的 python 对象.我最终看起来像:
This doesn't scale very well the more python objects. I end up with something looking like:
pattern_combined = '|'.join( [ first.pattern, second.pattern, third.pattern, etc ] )
关键是要连接的列表可能很长.任何想法如何避免这种混乱?提前致谢.
The point is that the list to concatenate can be very long. Any ideas how to avoid this mess? Thanks in advance.
推荐答案
我认为您不会找到不首先使用正则表达式对象创建列表的解决方案.我会这样做:
I don't think you will find a solution that doesn't involve creating a list with the regex objects first. I would do it this way:
# create patterns here...
re_first = re.compile(...)
re_second = re.compile(...)
re_third = re.compile(...)
# create a list with them
regexes = [re_first, re_second, re_third]
# create the combined one
pattern_combined = '|'.join(x.pattern for x in regexes)
当然,你也可以做相反的事情:组合模式然后编译,像这样:
Of course, you can also do the opposite: Combine the patterns and then compile, like this:
pattern1 = r'pattern-1'
pattern2 = r'pattern-2'
pattern3 = r'pattern-3'
patterns = [pattern1, pattern2, pattern3]
compiled_combined = re.compile('|'.join(x for x in patterns), FLAGS_TO_USE)
这篇关于连接正则表达式对象的 Pythonic 方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!