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

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

test = re.compile(
    ('(?P<full_path>.+):\d+:\s+warning:\s+Member'
     '\s+(?P<member_name>.+)\s+\((?P<member_type>%s)\) '
     'of (class|group|namespace)\s+(?P<class_name>.+)'
     '\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: String literal concatenation

Leave a Comment