Detailed Explanation of the Usage of Common Regular Functions in Python

  • 2021-12-05 06:27:45
  • OfStack

Today, I will introduce you to the regular expression processing functions commonly used in Python. There are two main methods for pattern matching in Python's regular expressions: "search" and "match."

re.match

re. match attempts to match a pattern in whole or in part from the beginning of a string, such as the following example matching the first word.  


import re
text = "PythonTab.com is a good Python website"
m = re.match(r"(\w+)\s", text)
if m:
print m.group(0), '\n', m.group(1)
else:
print 'not match'

The function prototype of re. match is: re. match (pattern, string, flags)

The first parameter is a regular expression, which is "(\ w +)\ s". If the match is successful, 1 Match will be returned, otherwise 1 None will be returned;

The second parameter represents the string to match;

The third parameter is the Peugeot bit, which controls how regular expressions are matched, such as case sensitivity, multi-line matching, and so on.

Note: The premise that it can be matched is that the matching conditions must be met at the beginning

re.search

The re. search function looks for a pattern match within a string until the first match is found and returns None if the string does not match.


import re
text = "PythonTab.com is a good Python website"
m = re.search(r'\Pyt(on)n\s', text)
if m:
print m.group(0), m.group(1)
else:
print 'not search'

The function prototype of re. search is: re. search (pattern, string, flags)

The meaning of each parameter is similar to that of re. match1.  

The difference between re. match and re. search: re. match only matches the beginning of a string. If the beginning of a string does not conform to a regular expression, the matching fails and the function returns None; And re. search matches the entire string until one match is found.

re.sub

re. sub is used to replace matches in strings. The following example replaces the space ''in the string with '-':


import re
text = "PythonTab.com is a good Python website"
print re.sub(r'\s+', '-', text)

The function prototype of re. sub is: re. sub (pattern, repl, string, count)

The second function is the replaced string; In this case, '-'

The fourth parameter refers to the number of replacements. The default is 0, which means that every match is replaced.

re. sub also allows for complex processing of matching substitutions using functions. 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 strings, such as re. split (r '\ s +', text); Split the string into a list of 1 words by space.

re.findall

re. findall can get all matching strings in a string. For example: re. findall (r '\ w*oo\ w*', text); Gets all the words in the string that contain 'oo'.

re.compile

You can compile a regular expression into a regular expression object. You can compile regular expressions that are often used into regular expression objects, which can improve the efficiency of 1. Here is an example of a regular expression object:


import re
text = "PythonTab is a good Python website"
regex = re.compile(r'\w*on\w*')
print regex.findall(text)   # Find all containing 'on' Words of 
print regex.sub(lambda m: '[' + m.group(0) + ']', text) # Contains a string containing 'on' Words used in [] Close it up. 

For more information on the regular expression handlers commonly used in Python, please click on the following articles


Related articles: