Python - Regular Expressions (re) Methods



The re module in Python provides powerful tools for working with regular expressions. Regular expressions, often abbreviated as regex or regexp, are sequences of characters that define a search pattern, used mainly for pattern matching within strings. This module is mainly used for text processing in Python. By using these methods, you can efficiently search, match, and manipulate strings based on complex patterns.

Let's explore the available methods in this module along with their descriptions −

Sr.No. Methods with Description
1

re.compile()

Compiles a regular expression into a pattern object.

2

re.search()

Scan through string looking for a match to the pattern./td>

3

re.match()

Checks if the regex pattern matches at the beginning of the string.

4

re.fullmatch()

Checks if the entire string matches the regex pattern.

5

re.split()

Splits the string by occurrences of the regex pattern.

6

re.findall()

Returns all non-overlapping matches of the regex pattern in the string.

7

re.finditer()

Returns an iterator over all non-overlapping matches in the string.

8

re.sub()

Replaces occurrences of the regex pattern in the string with the replacement string.

9

re.subn()

Similar to re.sub(), but returns a tuple containing the new string and the number of replacements made.

10

re.escape(pattern)

Escapes special characters in the regex pattern.

11

re.purge()

Clears the regular expression cache.

python_modules.htm
Advertisements