Python original string (raw strings) Usage instance _python

Source: Internet
Author: User
Tags character classes

This example describes the use of the Python raw Strings (raw) string for your reference. Specifically as follows:

The original Python string was created precisely because of the existence of regular expressions. The reason is the conflict between the ASCII character wildcards expressions and the special character of the expression. For example, the special symbol "\b" represents the backspace key in the ASCII character, but also "\b" is a special symbol of a regular expression that represents "matching a word boundary".

In order for the re compiler to use the two-character "\b" as the string you want to express instead of a backspace key, you need to escape it with another backslash, which can be written as "\\b".

But doing so complicates the problem, especially when you have a lot of special characters in your regular expression string, which makes it more confusing. In general, raw strings are often used to simplify the complexity of regular expressions.

In fact, many Python programmers use only raw strings when defining regular expressions.

The following example illustrates the difference between the backspace key "\b" and the regular expression "\b" (which contains or does not contain the original string):

Copy Code code as follows:
>>> m = re.match (' \bblow ', ' Blow ') # backspace, no match #退格键, no match >>> if M is not none:m.group ()
...
>>> m = re.match (' \\bblow ', ' Blow ') # escaped \, now it works #用 \ escaped, and it matches the
>>> if M not None:m.group ()
...
' Blow '
>>> m = Re.match (R ' \bblow ', ' Blow ') # Use raw string instead #改用原始字符串 >>> if m are not none:m.group ()
...
' Blow '

You may notice that we use "\d" in regular expressions, no original strings, no problems. That's because there's no special character in ASCII, so the regular expression compiler knows that you're referring to a decimal digit.

This feature of the original string makes some work very handy, such as the creation of regular expressions. Regular expressions are strings that define advanced search matching, usually consisting of special symbols that represent characters, groupings, matching information, variable names, and character classes. The regular expression module already contains enough symbols. But when you have to insert extra symbols to make special characters behave like regular characters, you're stuck in a "character number" Quagmire! Then the original string would come in handy.

In addition to the original string symbol (the letter "R" in front of the quotation mark), the original string has almost exactly the same syntax as the normal string. This ' r ' can be lowercase or uppercase, and the only requirement is that it must be immediately preceding the first quotation mark. In the 1th example of 3 examples, we need a backslash plus an "n" instead of a newline character.

Copy Code code as follows:
>>> ' \ n '
' \ n '
>>> print ' \ n '
>>> R ' n '
' \\n '
>>> print R ' \ n '
\ n

In the next example, we can't open our Readme file, why? Because ' \ t ' and ' \ R ' are considered special symbols that are not in our file names, they are actually 4 separate characters in the file path.
Copy Code code 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 to last update!) \ n '
>>> F.close ()

I hope this article will help you with your 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.