y***a 发帖数: 840 | 1 >>> import re
>>> re.sub(r'\$xxx', 'yyy', '$xxxxx')
'yyyxx'
为啥会MATCH&REPLACE?
我的理解是 r'\$xxx' 就是‘\$xxx', 所以'$xxxxx'不会MATCH,不应该有REPLACE发生
。 ANY COMMENT? THX
When an "r" or "R" prefix is present, a character following a backslash is
included in the string without change, and all backslashes are left in the
string. For example, the string literal r"\n" consists of two characters: a
backslash and a lowercase "n". String quotes can be escaped with a backslash
, but the backslash remains in the string; for example, r""" is a valid
string literal consisting of two characters: a backslash and a double quote;
r"" is not a valid string literal (even a raw string cannot end in an odd
number of backslashes). Specifically, a raw string cannot end in a single
backslash (since the backslash would escape the following quote character).
Note also that a single backslash followed by a newline is interpreted as
those two characters as part of the string, not as a line continuation. | l*********s 发帖数: 5409 | 2 first argument is pattern. | e*******o 发帖数: 4654 | 3 >>> re.sub(r'xxx$', 'yyy', '$xxxxx')
'$xxyyy'
>>> re.sub(r'\$xxx', 'yyy', '$xxxxx')
'yyyxx'
>>>
'$' is a metacharacter, in regex, match the end of string.
and '\$' match character '$' | y***a 发帖数: 840 | 4 according to "When an "r" or "R" prefix is present, a character following a
backslash is
included in the string without change, and all backslashes are left in the
string."
r"\$xxx" should be a string whose first character is "\" and 2nd is "$", but
$xxxxx doesn't have "\", therefore there should be no match.
Is my understanding of the prefix 'r' wrong? | l*********s 发帖数: 5409 | 5 you understand 'r' prefix correctly, but the regex engine sees '$' and
interprets it as'$'
a
【在 y***a 的大作中提到】 : according to "When an "r" or "R" prefix is present, a character following a : backslash is : included in the string without change, and all backslashes are left in the : string." : r"\$xxx" should be a string whose first character is "\" and 2nd is "$", but : $xxxxx doesn't have "\", therefore there should be no match. : Is my understanding of the prefix 'r' wrong?
| y***a 发帖数: 840 | 6 no. We are talking about different things.
my understanding is tthat the 'r' prefix will force the regex engine to
leave the backslack "\" in the pattern string, therefore $xxxxx cannot match
since it doesn't have the backslacsh
【在 l*********s 的大作中提到】 : you understand 'r' prefix correctly, but the regex engine sees '$' and : interprets it as'$' : : a
| l*********s 发帖数: 5409 | 7 ‘r'的功能就是help里说的那点, 你想多了
match
【在 y***a 的大作中提到】 : no. We are talking about different things. : my understanding is tthat the 'r' prefix will force the regex engine to : leave the backslack "\" in the pattern string, therefore $xxxxx cannot match : since it doesn't have the backslacsh
| y***a 发帖数: 840 | 8 HELP说的很模糊。你能解释一下为什么下面这两个一个MATCH 一个不MATCH吗?
>>> re.sub(r'\\$xxx', 'in' ,r'\\$xxxx')
'\$xxxx'
>>> re.sub(r'\$xxx', 'in' ,r'\$xxxx')
'\\\inx'
>>> r'\\$xxx' in r'\\$xxxx'
True
【在 l*********s 的大作中提到】 : ‘r'的功能就是help里说的那点, 你想多了 : : match
| l*********s 发帖数: 5409 | 9 because $ is a special character meaning the end, if it not escaped, $xxx
won't match anything.
【在 y***a 的大作中提到】 : HELP说的很模糊。你能解释一下为什么下面这两个一个MATCH 一个不MATCH吗? : >>> re.sub(r'\\$xxx', 'in' ,r'\\$xxxx') : '\$xxxx' : >>> re.sub(r'\$xxx', 'in' ,r'\$xxxx') : '\\\inx' : >>> r'\\$xxx' in r'\\$xxxx' : True
|
|