Summary and analysis of common methods of re module in Python

  • 2021-11-30 00:59:06
  • OfStack

re.findall()

Find all the substrings that the regular expression matches in the string and return a list, or an empty list if no match is found.

Syntax format:

re.findall(pattern, string, flags=0)

Parameters:
pattern Matches the regularity of the string.
string The string to be matched.

Example:

Requirement: Find all the numbers in the string


result = re.findall(r'\d+','1python2hello3')
print(result)

Results
['1', '2', '3']


result = re.findall(r'p%','helloP%ython')
print(result)

Results
[] # Returns null because the string to match P% and regular p% do not match


result = re.findall(r'p%','helloP%ython',re.I)
print(result)

Results
[P%]

re.sub()

Syntax:

re.sub(pattern, repl, string, count=0, flags=0)

Parameters:
pattern Pattern strings in a regular.
repl A substituted string, or a function.
string The original string to be found and replaced.
count Maximum number of replacements after pattern matching. The default 0 means replacing all matches.


result = re.sub(r'python','world','hellopython')
print(result)

Results
helloworld

The repl parameter is a function


# Requirements: Match to results less than 4 Replace with 8 , greater than 4 Replace with 9
import re
def modify(value):
    matched = value.group()
    if int(matched) <= 4:
        return '8'
    else:
        return '9'
str = 'ab12cd34ef567'
result = re.sub('\d', modify, str)
print(result )

Results
ab88cd88ef999

Usage scenario: You can operate on the part to be replaced after successful matching

re.compile

When we use regular expressions in Python, inside the re module:

1) Compile a regular expression. If the string of the regular expression itself is illegal, an error will be reported; 2) Use the compiled regular expression to match the string.

Then if a regular expression is to be reused thousands of times, for the sake of efficiency, should we precompile the regular first, and then when reusing it, we no longer need to compile this step, and match it directly to improve our efficiency

The compile function is used to compile the regular expression to produce a regular expression (Pattern) object for use by the match () and search () functions.

The syntax format is:

re.compile(pattern[, flags])

Parameters:
pattern : 1 regular expression in the form of a string
flags Optional, indicating matching patterns, such as ignoring case, multi-line pattern, etc.,


str = 'ab12cd34ef567'
info = re.compile(r'([a-z]{2})(\d{2})')
result = info.match(str).group()
result1 = info.match(str).groups() 
print(result)
print(result1)

Results
ab12
('ab', '12')

re.match

re. match attempts to match a pattern from the start of the string. If the start is not matched successfully, match () returns none.

Grammar

re.match(pattern, string, flags=0)

Parameters:
pattern  Matching regular expressions
string The string to match.
string0 Flag bit, which is used to control the matching method of regular expressions, such as case sensitivity, multi-line matching, etc.


str = 'ab12cd34ef567'
info = re.compile(r'([a-z]{2})(\d{2})')
result = info.match(str).group()
result1 = info.match(str).groups() 
print(result)
print(result1)

Results
ab12
('ab', '12')


str1 = '0b12cd34ef567'
result1 = info.match(str1).group()
print(result1)

Results: Errors will be reported
AttributeError: 'NoneType' object has no attribute 'group'

re.search

re. search scans the entire string and returns the first successful match.

Parameters:
pattern  Matching regular expressions
string The string to match.
flags Flag bit, which is used to control the matching method of regular expressions, such as case sensitivity, multi-line matching, etc.


str = 'ab12cd34ef567'
info = re.compile(r'([0-9]{2})')
result = info.search(str).group()
print(result)

Results
12

Differences between re. match and re. search

re. match only matches the beginning of the string. If the beginning of the string does not match the regular expression, the match fails and the function returns None;

re. search matches the entire string until one match is found.

re.split

The split method splits the string according to the matching substring and returns it to the list

Grammar

re.split(pattern, string[, maxsplit=0, flags=0])

Parameters:
pattern Matching regular expressions
string The string to match.
flags Flag bit, which is used to control the matching method of regular expressions, such as case sensitivity, multi-line matching, etc.


str = 'abc def g hi'
result = re.split('\W+',str)
print(result)

Results
['abc', 'def', 'g', 'hi']

The above is the Python re module commonly used method summary analysis of the details, more about Python re module commonly used method information please pay attention to this site other related articles!


Related articles: