编写跨越多行的正则表达式

时间:2019-04-24 09:00:55

标签: python regex

我有几种正则表达式模式,它们变得如此长而复杂,以至于难以阅读和维护。我有办法在功能保持完好的情况下以多行缩进方式编写它们吗?

例如,其中之一类似于:

pattern = r'^(.*(xxx|xxx|xxx|xxx|xxx|(xxx.*?xxx)|xxx|xxx|xxx|xxx|xxx|[xxxxxxxxxxxx\d]+(xxx|xxx|xxx|xxx[xxxxxxxxx\d]+xxx|xxx)|[xxxxxxxxx]+xxx|xxx|xxx))+(?!(xxx|xxx))([xxxxxxxxxxxxxxxxxx\d]+(?!([\da-zA-Z\-\,\/xxxxxxxxxxxx]|xxx))(xxx|xxx|xxx|xxx|xxx|xxx)+)*'

我想知道是否有这样一种写法:

pattern = r'''^(.*
                    (
                        xxx|xxx|xxx|xxx|xxx|(xxx.*?xxx)|xxx|xxx|xxx|xxx|xxx|
                        [xxxxxxxxxxxx\d]+(xxx|xxx|xxx|xxx[xxxxxxxxx\d]+xxx|xxx)|
                        [xxxxxxxxx]+xxx|xxx|xxx
                    )
                )+
                (?!(xxx|xxx))
                (
                    [xxxxxxxxxxxxxxxxxx\d]+
                    (?!([\da-zA-Z\-\,\/xxxxxxxxxxxx]|xxx))
                    (xxx|xxx|xxx|xxx|xxx|xxx)+
                )*'''

谢谢。

0 个答案:

没有答案