python regular expression “\1”

前端 未结 5 1515
伪装坚强ぢ
伪装坚强ぢ 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:07

    From the python docs for the re module:

    \number

    Matches the contents of the group of the same number. Groups are numbered starting from 1. For example, (.+) \1 matches 'the the' or '55 55', but not 'thethe' (note the space after the group). This special sequence can only be used to match one of the first 99 groups. If the first digit of number is 0, or number is 3 octal digits long, it will not be interpreted as a group match, but as the character with octal value number. Inside the '[' and ']' of a character class, all numeric escapes are treated as characters.

    Your example is basically the same as what is explained in the docs.

提交回复
热议问题