网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

[python]rawstring,反斜杠\,reLib

import re
print('\ a:{}个字符,\\a:{}个字符'.format(len('\ a'),len('\a')))
#结果:  \ a:3个字符,\a:1个字符

match2 = re.findall('\\\\','\ a')
match3 = re.findall('\\\\','\a')
match4 = re.findall('\\a','\a')
print(match2,match3,match4)
print(match2[0],match4[0])
#结果:  ['\\'] [] ['\x07']
#结果:  \ 

match5 = re.findall(r'\\','\ a')
match6 = re.findall(r'\\','\a')
match7 = re.findall(r'\a','\a')
print(match5,match6,match7)
#结果:  ['\\'] [] ['\x07']

创新互联成立与2013年,先为涿鹿等服务建站,涿鹿等地企业,进行企业商务咨询服务。为涿鹿企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

第一段:在字符串中,\a为一个字符,\空格为两个字符。

第二段:在字符串中,无法搜索出\a字符中单独的反斜杠\,需要整体字符搜索。当需要搜索单独的一个反斜杠\时,需要使用两个\来搜索,即使用一个转义符\来表示反斜杠本身,re中pattern则要写为'\\\\'或r'\\'。

第三段:r'\'相当于'\\',同理r'\\'相当于"\\\\"。

参考:https://docs.python.org/3/library/re.html#regular-expression-syntax

\

If you’re not using a raw string to express the pattern, remember that Python also uses the backslash as an escape sequence in string literals; if the escape sequence isn’t recognized by Python’s parser, the backslash and subsequent character are included in the resulting string. However, if Python would recognize the resulting sequence, the backslash should be repeated twice. This is complicated and hard to understand, so it’s highly recommended that you use raw stringsfor all but the simplest expressions.
如果你没有使用原始字符串来表达模式,请记住Python也使用反斜杠作为字符串文字中的转义序列; 如果Python的解析器无法识别转义序列,则反斜杠和后续字符将包含在结果字符串中。但是,如果Python识别出结果序列,则反斜杠应重复两次。这很复杂且难以理解,因此强烈建议您使用原始字符串,除了最简单的表达式。


网站名称:[python]rawstring,反斜杠\,reLib
当前路径:http://bjjierui.cn/article/jijhgp.html

其他资讯