python regular expression “\1”

前端 未结 5 1528
伪装坚强ぢ
伪装坚强ぢ 2020-12-24 01:47

Can anyone tell me what does \"\\1\" mean in the following regular expression in Python?

re.sub(r\'(\\b[a-z]+) \\1\', r\'\\1\', \'cat in the the hat\')
         


        
5条回答
  •  被撕碎了的回忆
    2020-12-24 02:15

    \1 is equivalent to re.search(...).group(1), the first parentheses-delimited expression inside of the regex.

    It's also, fun fact, part of the reason that regular expressions are significantly slower in Python and other programming languages than required to be by CS theory.

提交回复
热议问题