Compile functions using Python Functions

Source: Internet
Author: User
Tags expression engine

Next, we will introduce the compilation of other common Python functions in the module and use these functions flexibly to make full use of the powerful Python Regular Expression functions. After reading this article, you will certainly have a lot of GAINS, I hope this article will teach you more things.

Use the findall (rule, target) method to match the string. There is nothing to do once or twice. If it is used multiple times, the Regular Expression Engine must explain the rules once. The explanation of the rules is quite time-consuming, so the efficiency is very low. If you want to use the same rule multiple times for matching, you can use the re. compile function to pre-compile the rule and use the Regular Expression Object or Pattern Object that has been compiled for search.

Compile a regular expression using Python functions, not only to improve the matching speed, but also to use some additional functions. The compiled result generates a Pattern object. There are many functions in this object. They seem to be very similar to the Python function compilation of the re module. It also has functions such as findall, match, search, finditer, sub, subn, and split.

  • Analysis of Python Dynamic Language
  • Advantages of Python scripts
  • How to set the main Python thread
  • How to Use PythonPython to create a command?
  • Research on Python static Compiler

However, their parameters are slightly different. Generally, the first parameter of the re module function, that is, the regular expression rule is no longer needed. The rule should be included in the Pattern object, and the compilation option is no longer needed, because it has been compiled. Therefore, the positions of the two parameters in the re module are replaced by the following parameters.

The findall, match, search, and finditer functions have the same parameters. Apart from the absence of rules and options, they add the other two parameters. They are: find the start position and end position, that is, you can now specify the search range, except the range you are not interested in. Their current parameter format is:

 
 
  1. import Image  
  2.  
  3. # load a color image  
  4. im = Image.open(''fun.jpg'')  
  5.  
  6. # convert to grey level image  
  7. Lim = im.convert(''L'')  
  8. Lim.save(''fun_Level.jpg'')  
  9.  
  10. # setup a converting table with constant threshold  
  11. threshold = 80 
  12. table = []  
  13. for i in range(256):  
  14.     if i < threshold: 
  15.         table.append(0)  
  16.     else:  
  17.         table.append(1)  
  18.  
  19. # convert to binary image by the table  
  20. bim = Lim.point(table, ''1'')  
  21.  
  22. bim.save(''fun_binary.jpg'')  

Be careful. Because regular expressions use backslashes to escape special characters, when python itself processes strings, The backslashes are also used to escape characters, this leads to a double conversion problem. How do I write a regular expression to match one backslash in a string? "\", OK? Try it.

The re module throws an exception. The Python function is compiled because "\" is a backslash. For the regular expression parser, it is an escape character, but there is nothing behind it, naturally, an error is reported. The "\\\\" Three cannot be returned. Try four "\\\\" to make a perfect match.

Let's analyze this example: this regular expression matches a word, number, or an ID starting with a letter or '_' followed by a letter or number. The rules for these three cases are all packaged into a naming group named 'word', 'num', and 'id' respectively '. It is not case sensitive, so the compilation option is used.

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.