Analyze the special characteristics of Python Regular Expressions

Source: Internet
Author: User

The re module of Python makes many useful improvements to Python regular expressions. After reading this article, I believe you will be able to understand the special characteristics of Python in regular expressions, for example, what is the simplest regular expression.

Programmers who need to process text must have a comprehensive and in-depth understanding of regular expressions. This article provides a quick start tutorial for readers who are not familiar with regular expressions. Of course, this article is also helpful for some readers who are familiar with regular expressions in other languages, because you can understand the special characteristics of Python in regular expressions.

1. What is a Python regular expression?

When writing a program or webpage that processes strings, it is often necessary to find strings that conform to certain complex rules (or patterns. Regular Expressions are tools used to describe these rules (or patterns. In other words, a regular expression is the code that records text rules. Once the required text is found, you can modify it accordingly.

Remember the wildcards used for file search in the Windows command line, that is, * and ?. When we look for all the PDF files in a directory, we only need to search for *. pdf. Here, * is interpreted as any string. Like wildcards, regular expressions are also a tool for text matching. They can more accurately describe your needs than wildcards, for example, finding all phone numbers on a web page.

We know that telephone numbers generally have a fixed format: area code-telephone number, that is, a telephone number that starts with 0, followed by 2-3 numbers, and then a hyphen "-", A string consisting of 7 or 8 digits (for example, 010-12345678 or 0634-1234567 ).

2. The simplest Regular Expression

The best way to learn regular expressions is to start with a specific example and let the reader experiment in person. The following are some simple examples and detailed descriptions of them. When we look for to in a string, you can use the regular expression. This is almost the simplest regular expression, which can precisely match such a string;

It consists of two characters, the first character is t, and the last one is o. For demonstration, we provide a function re_show (), which can be considered as an encapsulation of the re module, it matches the given string (that is, a string matches a regular expression.

It usually refers to the content of this string that contains part or part of it or all of it meets the conditions given by the expression. We will not further introduce this function. You only need to know that the first parameter of re_show () is a Python regular expression, and the second parameter is the string to be matched. When the Matching content is found, just enclose it with curly brackets. The source code is as follows:

 
 
  1. import re  
  2. def re_show(pat, s):  
  3.     print re.compile(pat, re.M).sub("{\g<0>}", s.rstrip()),'\n'  
  4. s = '''Python runs on Windows, Linux/Unix,  
  5. Mac OS X, OS/2, Amiga, Palm Handhelds, and Nokia mobile phones.  
  6. Python has also been ported to the Java and .NET virtual machines.'''  
  7. re_show("to",s) 

The function calls re_show ("to", s) to find whether string s contains the string to, or whether string s matches the regular expression to. If yes, add curly brackets to the string.

3. Python Regular Expression matching words

We can see that the above regular expression is still acceptable if we only need to find the string to in the text, but if we want to match the word to in the text, the above regular expression to is not enough. For example, change the definition of string s in the above Code to the following:

 
 
  1. s = '''In company or association with respect to place or time;  
  2. as, to live together in one house; to live together in the  
  3. same age; they walked together to the town.''' 

We found that not only the word to, but also the word together and town contain the string, if the above Python regular expression to is used to find the "word" to, an error will occur.

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

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.