Two bugs in your code. First, you're not matching (and specifically, capturing) what you think you're matching and capturing -- insert after your call to .search:
>>> _.groups()
('',)
The unconstrained repetition of repetitions (star after a capturing group with nothing but stars) matches once too many -- with the empty string at the end of what you think you're matchin -- and that's what gets captured. Fix by changing at least one of the stars to a plus, e.g., by:
Now THIS matches and captures sensibly. Second, youre not using raw string literal syntax where you should, so you don't have a backslash where you think you have one -- you have an escape sequence \1 which is the same as chr(1). Fix by using raw string literal syntax, i.e. after the above snippet
Alternatively you could double up all of your backslashes, to avoid them being taken as the start of escape sequences -- but, raw string literal syntax is much more readable.
发布评论
评论(2)
您的代码中有两个错误。 首先,您没有匹配(特别是捕获)您认为正在匹配和捕获的内容 - 在调用
.search
之后插入:重复的无约束重复(捕获组后的星号)除了星星之外什么都没有)匹配一次太多——在你认为匹配的内容末尾有一个空字符串——这就是被捕获的内容。 通过将至少一颗星星更改为加号来修复,例如:
现在,这可以合理地匹配和捕获。 其次,您没有在应该使用的地方使用原始字符串文字语法,因此您认为有反斜杠的地方没有反斜杠——您有一个转义序列
\1
,它与 chr( 1)。 通过使用原始字符串文字语法进行修复,即在上面的代码片段之后或者您可以将所有反斜杠加倍,以避免它们被视为转义序列的开头 - 但是,原始字符串文字语法更具可读性。
Two bugs in your code. First, you're not matching (and specifically, capturing) what you think you're matching and capturing -- insert after your call to
.search
:The unconstrained repetition of repetitions (star after a capturing group with nothing but stars) matches once too many -- with the empty string at the end of what you think you're matchin -- and that's what gets captured. Fix by changing at least one of the stars to a plus, e.g., by:
Now THIS matches and captures sensibly. Second, youre not using raw string literal syntax where you should, so you don't have a backslash where you think you have one -- you have an escape sequence
\1
which is the same as chr(1). Fix by using raw string literal syntax, i.e. after the above snippetAlternatively you could double up all of your backslashes, to avoid them being taken as the start of escape sequences -- but, raw string literal syntax is much more readable.