Usage of 'r' before the string learned by Python

Source: Internet
Author: User
Tags character classes printable characters

6.4.3 original string operator (R/R)

The purpose of the original string has been described in python1.5 to deal
(The following section describes these special characters ). In the original string, all characters are directly followed by words
Without escaping special or printable characters.

This feature of the original string makes some work very convenient, such as the creation of regular expressions (see the re
Module). Regular Expressions are some strings that define the Advanced Search matching method, usually represented by characters, grouping, matching information
Information, variable name, and character class. The regular expression module already contains enough symbols. But when you
When extra characters must be inserted to make special characters look like common characters, you are in the quarary of "character numbers!
Then the original string will be used.
Except for the original string symbol (the letter "R" before the quotation marks), the original string is almost identical to the normal string.
Same syntax.
This 'R' can be lowercase or uppercase. The only requirement is that it must be placed before the first quotation mark.
In the first example of the three examples, we need a backslash and a 'n' instead of a line break .:

>>> '\ N'
'\ N'
>>> Print '\ N'

>>> R' \ N'
'\ N'
>>> Print R' \ N'
\ N

In the following example, we cannot open our readme file. Why? Because '\ t' and' \ R' are treated
It is not a special symbol in our file name, but they are actually four independent characters in the file path.

>>> F = open ('C: \ windows \ temp \ readme.txt ', 'R ')
Traceback (most recent call last ):
Edit by vheavens
Edit by vheavens
File "<stdin>", line 1, in?
F = open ('C: \ windows \ temp \ readme.txt ', 'R ')
Ioerror: [errno 2] No such file or directory: 'C: \ win-dows \ temp \ readme.txt'
>>> F = open (r'c: \ windows \ temp \ readme.txt ', 'R ')
>>> F. Readline ()
'Table of contents (please check timestamps for last update !) \ N'
>>> F. Close ()

Finally, we need to find a pair of original \ n characters instead of line breaks. To find it, we use a simple regular table.
It is used to find backslash-Character
Pairs ).

>>> Import re
>>> M = Re. Search ('\ [rtfvn]', r'hello world! \ N ')
>>> If M is not none: M. Group ()
...
>>> M = Re. Search (R' \ [rtfvn] ', r'hello world! \ N ')
>>> If M is not none: M. Group ()
...
'\ N'

Role of 'R' in Regular Expressions:

Core notes: usage of the python raw string (raw strings)
You may have seen some examples of the original string usage. The original string is generated because of the regular table.
Dashboard. The reason is the conflict between ASCII characters and special characters of the regular expression. For example, the special symbol "\ B" is
The ASCII character represents the return key, but "\ B" is also a special symbol of a regular expression, representing "matching a word boundary ".
In order for the Re compiler to treat the two characters "\ B" as the string you want to express, rather than a backspace key, you need to use another
Escape the backslash (\ B ".
However, this will complicate the problem, especially when your regular expression string contains many special characters.
It is easy to confuse. In chapter 6, we have introduced the original string, which is often used to simplify the complexity of regular expressions.
In fact, many Python programmers only use the original string when defining regular expressions.
The following example illustrates the difference between the backspace key "\ B" and the regular expression "\ B" (including or not including the original string:
>>> M = Re. Match ('\ bblow', 'blow') # backspace, no match # Return key, no match
>>> If M is not none: M. Group ()
...
>>> M = Re. Match ('\ bblow', 'blow') # escaped \, now it works # after escaping \,
Configured
>>> If M is not none: M. Group ()
...
'Blow'
>>> M = Re. Match (R' \ bblow', 'blow') # use raw string instead # use the original string instead.

'''

Note: '\ B' In the first parameter R' \ bblow' indicates the word boundary, but I don't understand why it represents the original string '\' and 'B,

Is this the most primitive?

However, the test result is indeed regarded as the word boundary. How can we express the two characters '\ B,

For example, how do I write a regular expression for a file starting with a letter B in the 'd: \ Documents folder? Refer to the following:

>>> Print '\ blow', R' \ blow', re. Search (R' \ blow', R' \ blow'). Group ()

Output result: low \ blow

To explain,Re. search (R' \ blow', R' \ blow '). in group (), the first two characters '\' are converted into a '\', and 'B' is the letter 'B'. Therefore, it matches the original character '\ blow'.

'''
>>> If M is not none: M. Group ()
...
'Blow'
You may notice that we use "\ D" in the regular expression, and there is no use of the original string, and there is no problem. That's because
There are no special characters in ASCII, so the regular expression compiler can know that you are referring to a decimal number.

 

 

 

'R' is used in a common string:

This feature of the original string makes some work very convenient, such as the creation of regular expressions (see the re module in the document ). A regular expression is a string that defines the Advanced Search matching method. It is usually composed of special characters, including characters, groups, matching information, variable names, and character classes. The regular expression module already contains enough symbols. But when you have to insert additional characters to make special characters look like normal characters, you are in the quarary of "character numbers! Then the original string will be used.

Except for the original string symbol (the letter "R" before the quotation marks), the original string has almost identical syntax with the normal string. This 'R' can be lowercase or uppercase. The only requirement is that it must be placed before the first quotation mark. In the first example of the three examples, we need a backslash and an "N" instead of a line break.

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on Win32
Type "Copyright", "Credits" or "License ()" for more information.
>>> Print 'abc \ ndef'
ABC
Def
>>> Print r'abc \ ndef'
ABC \ ndef
>>> If '\ n' = R' \ N ':
Print '='

=
>>>

 

6.4.3 original string operator (R/R)

The purpose of the original string has been described in python1.5 to deal
(The following section describes these special characters ). In the original string, all characters are directly followed by words
Without escaping special or printable characters.

This feature of the original string makes some work very convenient, such as the creation of regular expressions (see the re
Module). Regular Expressions are some strings that define the Advanced Search matching method, usually represented by characters, grouping, matching information
Information, variable name, and character class. The regular expression module already contains enough symbols. But when you
When extra characters must be inserted to make special characters look like common characters, you are in the quarary of "character numbers!
Then the original string will be used.
Except for the original string symbol (the letter "R" before the quotation marks), the original string is almost identical to the normal string.
Same syntax.
This 'R' can be lowercase or uppercase. The only requirement is that it must be placed before the first quotation mark.
In the first example of the three examples, we need a backslash and a 'n' instead of a line break .:

>>> '\ N'
'\ N'
>>> Print '\ N'

>>> R' \ N'
'\ N'
>>> Print R' \ N'
\ N

In the following example, we cannot open our readme file. Why? Because '\ t' and' \ R' are treated
It is not a special symbol in our file name, but they are actually four independent characters in the file path.

>>> F = open ('C: \ windows \ temp \ readme.txt ', 'R ')
Traceback (most recent call last ):
Edit by vheavens
Edit by vheavens
File "<stdin>", line 1, in?
F = open ('C: \ windows \ temp \ readme.txt ', 'R ')
Ioerror: [errno 2] No such file or directory: 'C: \ win-dows \ temp \ readme.txt'
>>> F = open (r'c: \ windows \ temp \ readme.txt ', 'R ')
>>> F. Readline ()
'Table of contents (please check timestamps for last update !) \ N'
>>> F. Close ()

Finally, we need to find a pair of original \ n characters instead of line breaks. To find it, we use a simple regular table.
It is used to find backslash-Character
Pairs ).

>>> Import re
>>> M = Re. Search ('\ [rtfvn]', r'hello world! \ N ')
>>> If M is not none: M. Group ()
...
>>> M = Re. Search (R' \ [rtfvn] ', r'hello world! \ N ')
>>> If M is not none: M. Group ()
...
'\ N'

Role of 'R' in Regular Expressions:

Core notes: usage of the python raw string (raw strings)
You may have seen some examples of the original string usage. The original string is generated because of the regular table.
Dashboard. The reason is the conflict between ASCII characters and special characters of the regular expression. For example, the special symbol "\ B" is
The ASCII character represents the return key, but "\ B" is also a special symbol of a regular expression, representing "matching a word boundary ".
In order for the Re compiler to treat the two characters "\ B" as the string you want to express, rather than a backspace key, you need to use another
Escape the backslash (\ B ".
However, this will complicate the problem, especially when your regular expression string contains many special characters.
It is easy to confuse. In chapter 6, we have introduced the original string, which is often used to simplify the complexity of regular expressions.
In fact, many Python programmers only use the original string when defining regular expressions.
The following example illustrates the difference between the backspace key "\ B" and the regular expression "\ B" (including or not including the original string:
>>> M = Re. Match ('\ bblow', 'blow') # backspace, no match # Return key, no match
>>> If M is not none: M. Group ()
...
>>> M = Re. Match ('\ bblow', 'blow') # escaped \, now it works # after escaping \,
Configured
>>> If M is not none: M. Group ()
...
'Blow'
>>> M = Re. Match (R' \ bblow', 'blow') # use raw string instead # use the original string instead.

'''

Note: '\ B' In the first parameter R' \ bblow' indicates the word boundary, but I don't understand why it represents the original string '\' and 'B,

Is this the most primitive?

However, the test result is indeed regarded as the word boundary. How can we express the two characters '\ B,

For example, how do I write a regular expression for a file starting with a letter B in the 'd: \ Documents folder? Refer to the following:

>>> Print '\ blow', R' \ blow', re. Search (R' \ blow', R' \ blow'). Group ()

Output result: low \ blow

To explain,Re. search (R' \ blow', R' \ blow '). in group (), the first two characters '\' are converted into a '\', and 'B' is the letter 'B'. Therefore, it matches the original character '\ blow'.

'''
>>> If M is not none: M. Group ()
...
'Blow'
You may notice that we use "\ D" in the regular expression, and there is no use of the original string, and there is no problem. That's because
There are no special characters in ASCII, so the regular expression compiler can know that you are referring to a decimal number.

 

 

 

'R' is used in a common string:

This feature of the original string makes some work very convenient, such as the creation of regular expressions (see the re module in the document ). A regular expression is a string that defines the Advanced Search matching method. It is usually composed of special characters, including characters, groups, matching information, variable names, and character classes. The regular expression module already contains enough symbols. But when you have to insert additional characters to make special characters look like normal characters, you are in the quarary of "character numbers! Then the original string will be used.

Except for the original string symbol (the letter "R" before the quotation marks), the original string has almost identical syntax with the normal string. This 'R' can be lowercase or uppercase. The only requirement is that it must be placed before the first quotation mark. In the first example of the three examples, we need a backslash and an "N" instead of a line break.

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on Win32
Type "Copyright", "Credits" or "License ()" for more information.
>>> Print 'abc \ ndef'
ABC
Def
>>> Print r'abc \ ndef'
ABC \ ndef
>>> If '\ n' = R' \ N ':
Print '='

=
>>>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.