Python raw string (raw strings) Usage instance

Source: Internet
Author: User
Tags character classes
This article describes the Python raw string (raw strings) usage and shares it for everyone's reference. Specific as follows:

The creation of a Python primitive string is due to the existence of a regular expression. The reason is that the ASCII character wildcards regular the conflicts that arise between the special characters of the expression. For example, the special symbol "\b" represents the backspace key in the ASCII character, but at the same time "\b" is also a special symbol of a regular expression, which means "match 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 you can write: "\\b".

But doing so complicates the problem, especially if you have a lot of special characters in your regular expression string. In general, primitive strings are often used to simplify the complexity of regular expressions.

In fact, many Python programmers use only the original string when defining a regular expression.

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

The code is 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, matches the
>>> if M is 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, that there is no original string, and that there is no problem. That's because there are no special characters in ASCII, so the regular expression compiler knows you're referring to a decimal number.

This feature of the original string makes some work very convenient, such as the creation of regular expressions. Regular expressions are strings that define advanced search matches, usually consisting of special symbols representing 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 ordinary characters, you get stuck in the "character numbers" Quagmire! This is where the original string comes in handy.

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

The code is 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 treated as special symbols that are not in our filenames, they are actually 4 separate 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 ()

Hopefully this article will help you with Python programming.

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