Python original string (rawstrings) usage example

Source: Internet
Author: User
Tags character classes
This article mainly introduces the usage example of the Python original string (rawstrings), which is very useful in the process of string processing using Python, for more information about the usage of the original Python string (raw strings), see the following example. 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:

The 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.

The 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.

The 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.

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.