Today I learned about regular expressions in Python. I have not explained much about the regular expression syntax. I have learned a lot on the Internet. This section describes common Regular Expression Processing functions in Python.
Re. Match
Re. MatchStart of stringMatch a pattern. For example, the following example matches the first word.Import Re </P> <p> text = "jgood is a handsome boy, he is cool, clever, and so on... "<br/> M = Re. match (R "(/W +)/s", text) <br/> If M: <br/> Print M. group (0), '/N', M. group (1) <br/> else: <br/> Print 'not Match'
Re. Match function prototype: Re. Match (pattern, String, flags)
The first parameter is a regular expression. Here it is "(/W +)/S". If the match is successful, a match is returned; otherwise, a none is returned;
The second parameter indicates the string to be matched;
The third parameter is the Peugeot bit, which is used to control the matching mode of regular expressions, such as case-sensitive or multi-line matching.
Re. Search
The Re. Search function searches for the pattern match in the string, only to find the first match and then returns it. If the string does not match, none is returned.Import Re </P> <p> text = "jgood is a handsome boy, he is cool, clever, and so on... "<br/> M = Re. search (R'/Shan (DS) ome/s', text) <br/> If M: <br/> Print M. group (0), M. group (1) <br/> else: <br/> Print 'not search'
Re. Search function prototype: Re. Search (pattern, String, flags)
The meaning of each parameter is the same as that of RE. Match.
The difference between re. Match and RE. Search:Re. Match only matches the start of the string. If the start of the string does not conform to the regular expression, the match fails, and the function returns none; and RE. Search matches the entire string until a match is found.
Re. sub
Re. sub is used to replace the match in the string. In the following example, replace space '''-':Import Re </P> <p> text = "jgood is a handsome boy, he is cool, clever, and so on... "<br/> Print re. sub (r '/S +', '-', text)
Re. sub function prototype: Re. sub (pattern, REPL, String, count)
The second function is the replaced string. In this example, It is '-'
The fourth parameter indicates the number of replicas. The default value is 0, indicating that each matching item is replaced.
Re. sub also allows the use of functions to replace matching items for complex processing. For example, re. sub (r '/s', Lambda M:' ['+ M. group (0) + ']', text, 0); replace the space ''In the string with '[]'.
Re. Split
You can use re. Split to split a string, such as Re. Split (r '/S +', text). The string is split into a word list by space.
Re. findall
Re. findall can obtain all matching strings in the string. For example, re. findall (R'/W * oo/W * ', text); obtain all words containing 'oo' in the string.
Re. Compile
You can compile a regular expression into a regular expression object. You can compile regular expressions that are frequently used into regular expression objects to improve efficiency. The following is an example of a regular expression object:
Import Re </P> <p> text = "jgood is a handsome boy, he is cool, clever, and so on... "<br/> RegEx = Re. compile (R'/W * oo/W * ') <br/> Print RegEx. findall (text) # search for all words containing 'oo '<br/> Print RegEx. sub (lambda M: '[' + M. group (0) + ']', text) # enclose Words Containing 'oo 'in.
For more details, refer to the python manual.