Python original string (raw strings) usage instance, pythonstrings
This article describes the usage of the Python raw string (raw strings) for your reference. The details are as follows:
The original Python string is generated due to the existence of regular expressions. The reason is the conflict between ASCII characters and special characters of the regular expression. For example, the special symbol "\ B" represents the return key in ASCII characters, but "\ B" is also a special symbol of a regular expression, representing "matching a word boundary ".
In order for the RE compiler to regard the two characters "\ B" as the string you want to express, rather than a backspace key, you need to use another backslash to escape it, that is, you can write it like this: \ B ".
But this will complicate the problem, especially when your regular expression string contains many special characters, it is more confusing. In general, the original string 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:
Copy codeThe Code is as follows: >>> m = re. match ('\ bblow', 'blow') # backspace, no match # backspace key, no match >>> if m is not None: m. group ()
...
>>> M = re. match ('\ bblow', 'blow') # escaped \, now it works # after escaping with \, it now matches
>>> If m is not None: m. group ()
...
'Blow'
>>> M = re. match (R' \ bblow', 'blow') # use raw string instead # use the original string >>> 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.
This feature of the original string makes some work very convenient, such as the creation of regular expressions. 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.
Copy codeThe Code is as follows: >>> '\ 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 as special characters not in our file name, but they are actually four independent characters in the file path.
Copy codeThe Code is as follows: >>> f = open ('C: \ windows \ temp \ readme.txt ', 'R') Traceback (most recent call last ):
File "", 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 ()
I hope this article will help you with Python programming.
Python original string matching
In fact, you only need to take a look at the content of rhas and has, and print the content to see the differences
In rhas, \ n is preceded by r. It does not mean that the carriage return is a line break, but it is a character \ or n. To match, enter r "hello \ n \ nworld" or "hello \ n \ nworld"
I think what puzzles you should be that t and s are in the same match with has. Why? This is mainly because the re will re-parse the string, and the character \ n in t will be converted to the carriage return when re-parsing.
If s = 'Hello \ n \ nworld', it can also match has.
In the same case, t = r "hello \ n \ nworld" or "hello \ n \ nworld" can also match rhas.
Python original string matching
R'... '. Here, r indicates that \ in the subsequent string does not represent the escape character.
In this way, your rhas actually contains the \ character, but there is only one line break.
Why can both the format string with and without match has? If the format string does not contain r, it contains two linefeeds instead \, in this way, it is exactly the same as the content of has, and it naturally matches. When it comes to r, the \ in it is actually recognized as an escape character in the Rule expression, in this way, expression matching can also match has (but t is not equal to has, but a format string with escape characters ).
When talking about how to match rhas, because rhas contains \ and this \ is also an escape character in the Rule expression, you can only use the format string of the Rule expression for matching. t can be written: r 'Hello \ n \ nworld', where \ is used as an escape character in the Rule expression to match a \ character. Split each part of the format string to correspond to rhas as follows:
Format: hello \ n world
Rhas: hello \ n newline world