python字符串前面加r的问题,不会影响\d这个转义字符

作者&投稿:帛真 (若有异议请与网页底部的电邮联系)
刚开始学python,遇到一个字符串前面加r的问题

理论上,字符串前面加r,会消除转义字符对字符串的影响

例:

s=r'\tt'

print(s)

Output:

'\tt'

s='\tt'

print(s)

Output:

'        t'

但是我发现对\d这个转义字符是没影响的

例如

import re

def re_method():

s ='kjiabc5ty'

    print(re.search(r'abc\d',s).group())

if __name__ =='__main__':

re_method()

依然可以匹配到abc5,并输出

我百思不得其解

后来在谷歌上搜到答案,大致意思说的\d不是有效的转义序列,所以python不会更改它,所以'\d' == r'\d'是对的。由于\\  是 有效的转义序列,因此将其更改为\,因此您得到了该行为'\d' == '\\d' == r'\d'。所以,字符串有时会造成混乱。

下面我粘贴一段原话

There is a distinction you have to make between the python interpreter and the re module.

In python, a backslash followed by a character can mean a special character if the string is not rawed. For instance, \n will mean a newline character, \r will mean a carriage return, \t will mean the tab character, \b represents a nondestructive backspace. By itself, \d in a python string does not mean anything special.

In regex however, there are a bunch of characters that would otherwise not always mean anything in python. But that's the catch, 'not always'. One of the things that can be misinterpreted is \b which in python is a backspace, in regex means a word boundary. What this implies is that if you pass on an unrawed \b to the regular expression part of a regex, this \b gets substituted by the backspace  before  it is passed to the regex function and it won't mean a thing there. So you have to absolutely pass the b with its backslash and to do that, you either escape the backslash, or raw the string.

Back to your question regarding \d, \d has no special meaning whatsoever in python, so it remains untouched. The same \d passed as a regular expression gets converted by the regex engine, which is a separate entity to the python interpreter.

翻译过来

您必须在python解释器和re模块之间进行区分。

在python中,如果未原始字符串,则反斜杠后跟一个字符可以表示一个特殊字符。例如,\n表示换行符,\r表示回车,\t表示制表符,\b表示无损退格键。就其本身而言,\d在python字符串中并不表示任何特殊含义。

但是在regex中,有一堆字符在python中并不总是意味着任何东西。但这很重要,“并非总是如此”。可能被误解的一件事是\b在python中是退格,在正则表达式中是单词边界。这意味着如果您将未展开\b的正则表达式部分传递给正则表达式,则在将其传递给regex函数 之前, 它\b会被退格键所替代,并且 在 此处不会有任何意义。因此,您必须绝对传递b带有反斜杠的,然后要么转义反斜杠,要么原始字符串。

回到关于的问题\d,\d在python中没有任何特殊含义,因此保持不变。同样\d为正则表达式通过得到由正则表达式引擎,这是一个单独的实体来python解释转换。

总之,我还是理解不太清楚,但是总算知道了有这回事。

再次记录一下,

~

python正则表达式问题,如图所示,为什么少了一个r结果会不同
答:字符串前面加上 r 表示原生字符串(rawstring)正则表达式中使用“\”作为转义字符,即如果是“\n”那么表示换行,如果r'\n'表示一个反斜杠字符,一个字母n,而不是表示换行了。python3.8.2

python中 r'', b'', u'', f'' 的含义
答:1、字符串前+u,如u"我是张三":前缀u表示该字符串是unicode编码,在Python2中常被用在中文字符的字符串前,防止因为编码问题所导致的乱码,一般在文件开头标明编码方式采用utf8。在Python3中,所有的字符串默认都是unicode字符串。2、字符串前+r,如r"\n\n\n\n\n":表示一个正常的字符串,通...

python如何给字符串前加r?
答:字母前加r表示raw string,也叫原始字符串常量。一般用在一下两个方面:1、正则表达式 用于处理正则表达式时,规避反斜杠的转义 2、系统路径 如下面的路径,使用r就防止了\t的转义

python字符串前缀 u和r的区别
答:一个字母n,而不是表示换行了。以r开头的字符,常用于正则表达式,对应着re模块。以u或U开头的字符串表示unicode字符串 Unicode是书写国际文本的标准方法。如果你想要用非英语写文本,那么你需要有一个支持Unicode的编辑器。类似地,Python允许你处理Unicode文本——你只需要在字符串前加上前缀u或U。

python中字符串前r的用法疑问
答:这个是表示不转义,使用真实字符 比如如下代码:s = r'test\tddd's2 = 'test\tddd'print(s)print(s2)输出结果是:test\tddd test ddd 其中s里面的\t就是使用真实字符\t,而不是转义为制表符

python脚本中为什么要在目录前加一个r?
答:r是保持字符串原始值的意思,就是说不对其中的符号进行转义。因为windows下的目录字符串中通常有斜杠"\",而斜杠在Python的字符串中有转义的作用。例如:\n表示换行如果路径中有\new就会被转义。加上r就是为了避免这种情况。

Python 字符串使用r开头,反斜杠防止转义,但是如果需要组合变量怎么弄...
答:和另一位说的一样,用反斜杠,另外补充一下可以用os.path.join来连接:import osos.path.join('c:/windows/', 'a_path_new', 'a_sub_path', 'file.txt')在合适的操作系统下会补充合适的斜杠的。

Python程序WinError 123,267?
答:找到上下文赋值字符串的地方,字符串前面加r。比如:folder_path =r'D:\123\test'。加r的目的是告诉编译器(解释器)这是一个字符串不是转义符号。

python正则表达式re.findall(r"\b\w+\b", s)中的r是什么意思?
答:在Python的string前面加上‘r’, 是为了告诉编译器这个string是个raw string,不要转意backslash '\' 。 例如,\n 在raw string中,是两个字符,\和n, 而不会转意为换行符。由于正则表达式和 \ 会有冲突,因此,当一个字符串使用了正则表达式后,最好在前面加上'r'。例:r"\n\n\n\n\n...

python os.system命令求助
答:而如果在一个字符串中想表示反斜杠本身,就需要先用转义符转义,就像这样——'\\'(没有外面的引号)。你的代码改成这样:os.system('zip -qr D:\\Development\\Python\\Temp\\20110825212607.zip D:\\Development\\Python\\HelloWorld')或者还有一种方法,就是字符串之前加上一个字母r,表示该...