由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - python 小问题
相关主题
入门问题,perl里s@\_@@g 是什么意思?正则表达式里括号的转义好乱
store "" in c stringcompare string ignore case
Question on using ## in #definenode- mongoose的思考
perl 的问题C++ string to int Problem
a MatLab questionchar *p = "string literal"; 和 char a[] = "string liter (转载)
Python没有for loop[合集] c++的题
error of opening a file located in a remote server from pyt (转载)问个缺少逗号的数组赋值问题
perl 匹配问题Need help on choosing the language
相关话题的讨论汇总
话题: string话题: backslash话题: match话题: xxx话题: character
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
Need help on choosing the languagea MatLab question
I like this one.Python没有for loop
expression in Javaerror of opening a file located in a remote server from pyt (转载)
一个c++小问题perl 匹配问题
入门问题,perl里s@\_@@g 是什么意思?正则表达式里括号的转义好乱
store "" in c stringcompare string ignore case
Question on using ## in #definenode- mongoose的思考
perl 的问题C++ string to int Problem
相关话题的讨论汇总
话题: string话题: backslash话题: match话题: xxx话题: character