The `re` module in Python provides regular expression matching operations similar to those found in Perl. Regular expressions use special characters to match or find other strings or sets of strings, using a specialized syntax held in a pattern.
Here are several things you can do with Python’s `re` module:
Import re module
import re1. Searching
This method searches for first occurrence of RE pattern within string with optional flags.
```
str = ‘hello world‘
x = re.search(‘o’, str)
print(x) # Output:
```
1. Matching
This method attempts to match RE pattern to string with optional flags.
```
str = ‘hello world‘
x = re.match(‘hello’, str) # Checks for matching pattern at the start of the string
print(x) # Output:
```
If the match is not found at the start of the string, ‘None’ is returned.
```
str = ‘hello world‘
x = re.match(‘world’, str)
print(x) # Output: None
```
1. Splitting
This method splits string by the occurrences of a character or a pattern, upon finding that pattern, the remaining characters from the string are returned as part of the resulting list.
```
str = ‘hello world‘
x = re.split(‘\s’,str)
print(x) # Output: [‘hello’, ‘world’]
```
1. Replacing
This method replaces all occurrences of the RE pattern in string with a given string, substituting all occurrences unless max provided.
```
str = ‘hello world‘
x = re.sub(‘\s’,’-’,str) # replace spaces with dashes
print(x) # Output: hello-world
```
1. Find all
This method returns all non-overlapping matches of pattern in string, as a list of strings.
```
str = ‘hello world‘
x = re.findall(‘o’,str)
print(x) # Output: [‘o’, ‘o’]
```
These are a few examples of how you can use the `re` module for handling regular expressions in Python. There is much more you can do with it.
Here are some resources where you can learn more:
- [Python docs](https://docs.python.org/3/library/re.html)
- [Python re module](https://www.w3schools.com/python/python_regex.asp)