How to split long regular expression rules to multiple lines in Python

前端 未结 6 1866
日久生厌
日久生厌 2020-11-30 02:58

Is this actually doable? I have some very long regex pattern rules that are hard to understand because they don\'t fit into the screen at once. Example:

test         


        
6条回答
  •  庸人自扰
    2020-11-30 03:33

    You can split your regex pattern by quoting each segment. No backslashes needed.

    test = re.compile(('(?P.+):\d+:\s+warning:\s+Member'
                       '\s+(?P.+)\s+\((?P%s)\) '
                       'of (class|group|namespace)\s+(?P.+)'
                       '\s+is not documented') % (self.__MEMBER_TYPES), re.IGNORECASE)
    

    You can also use the raw string flag 'r' and you'll have to put it before each segment.

    See the docs.

提交回复
热议问题