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:
- import Image
-
- # load a color image
- im = Image.open(''fun.jpg'')
-
- # convert to grey level image
- Lim = im.convert(''L'')
- Lim.save(''fun_Level.jpg'')
-
- # setup a converting table with constant threshold
- threshold = 80
- table = []
- for i in range(256):
- if i < threshold:
- table.append(0)
- else:
- table.append(1)
-
- # convert to binary image by the table
- bim = Lim.point(table, ''1'')
-
- 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.