Bases of Python: string and a regular expression backslash ( "\") Problems Explanation

We are about to finish Python strings and regular expressions backslash ( '\') issues and related knowledge, there is a need friends can learn under.

Bases of Python: string and a regular expression backslash ( "\") Problems Explanation

In general string Python

In Python, we use '\' to escape some ordinary characters, making it a special character, such as

1 In [1]: print('abc\ndef') # '\n'具有换行的作用
2 abc
3 defg
4 
5 In [2]: print('abc\tdef') # '\t'具有制位符的作用 
6 abc defg

We can also use '\' to escape special characters, making it a general character, such as

1 In [3]: print('abc\\tdef') # 使'\'成为一个普通的字符,没有转义作用
2 abc\tdef
3 
4 In [4]: print('abc\'tdef') # 使'''成为一个普通的字符,不再需要和另一半单引号'''配对 
5 abc'tde

In Python native string so that the string python general, we can not end with an odd number of '\', this will lead to the end of the quote character escape into a pure, so that this end of the string without quotes.

We add a string before 'r' to represent the original string, such r'abc '. Special meaning native string of special characters will be deprived, as follows

在学习过程中有什么不懂得可以加我的
python学习交流扣扣qun,×××
群里有不错的学习视频教程、开发工具与电子书籍。
与你分享python企业当下人才需求及怎么从零基础学习好python,和学习什么内容
1 In [5]: print(r'abc\ndefg') # '\n'没有换行的作用,仅仅是一个普通的字符串 
2 abc\ndefg
3 
4 In [6]: print(r'abc\tdefg') # '\t'没有制位符的作用,仅仅是一个普通的字符串 
5 abc\tdef

There is a strange question, in the native string, although '\' has become a normal string, but we also can not end with an odd number of '\' string, this is because of Python regular expressions, in regular expression we will explain this.

In Python (regular express) regular expression

Python regular expressions will have some differences, the regular expression specified in some meta character (metacharacter), as follows

1 . ^ $ * + ? { } [ ] \ | ( )

They all have a special meaning, '\' also belong to the meta-characters. '\' Belong to both Python special characters, are also metacharacters in regular, so when using very careful. So what's in a regular, a '\' role is it? We see an example

1 In [7]: re.search('.', 'sd.f') 
2 Out[7]: <_sre.SRE_Match object; span=(0, 1), match='s'>
3 
4 In [8]: re.search('\.', 'sd.f') 
5 Out[8]: <_sre.SRE_Match object; span=(2, 3), match='.'>In [9]: re.search('\n', 'sdfd\nfds') Out[9]: <_sre.SRE_Match object; span=(4, 5), match='\n'>
6 
7 """
8 '.'作为一个元字符,它的特殊含义是匹配任意一个字符,而当我们在其前面加'\'后,它的特殊含义消失了,匹配到的是'.';而且'\'依然可以把Python中某些字符转义成特殊字符。
9 """

In regular, the '\' the role of the meta character escaping into Python string literal (a Python string literal), so regular in the '\' in fact, is actually a Python character in '\', it still also has escape the role, so we have a regular for each '\' escape once again, that '\\', so that you can match the pure character '\' up. So look at the following example, the role of '\' is deprived of the special meaning metacharacters, making it a Python character, so it still has a role in the escape ordinary Python string, such as '\ n' still represents a newline. So, '\' is both a regular in a meta-character, but also a special character in Python. So how do we match the pure character '\' (without any special meaning)?

1 In [10]: re.search('\\\\', 'fsd\nfds') # 目标字符串中没有纯字符'\',所以匹配为空
2 
3 In [11]: re.search('\\\\', 'fsd\\nfds') 
4 Out[11]: <_sre.SRE_Match object; span=(3, 4), match='\\'> # Python字符串中,'\\'就是纯字符'\'

In the repeated re '\', which may generate a number of consecutive backslash, resulting in difficult to understand certain strings. The solution is to use native Python strings to write a regular expression, as

1 In [12]: re.search(r'\\', 'fsd\\nfds') # 两个'\\'就可以匹配到纯字符'\'
2 Out[12]: <_sre.SRE_Match object; span=(3, 4), match='\\'>

In addition, when we use native strings to write a regular expression, Python character special characters is still valid, such as

1 In [13]: re.search(r'\n', 'fsd\nfds') 
2 Out[13]: <_sre.SRE_Match object; span=(3, 4), match='\n'>
"""
在Python字符中,r'\n'仅仅表示纯字符串'\n',没有换行的作用;在正则中,r'\n'依然还有换行的作用。所以此时我们不能用奇数个'\'来结尾某个正则表达式,这样导致结束的引号被转义成一个纯字符,无结束符号。所以,在Python原生字符串中,我们也同样规定不能用奇数个'\'来结尾。
"""

That's all knowledge of the contents of this presentation, if you can find the article also wish to point a praise, have any comments or opinions are welcome to comment!

Guess you like

Origin blog.51cto.com/14568144/2444889