5.3. RegEx Functions

In this lesson, we will see some basic regex functions and how to use them. You can find some more advanced functions in the re documentation.

re.search()

After importing the module re, we can use re.search() to search the first location where this regular expression produces a match. Some useful methods are:

.span() returns a tuple containing the start-, and end positions of the match.

.group() returns the part of the string where there was a match.

> Here, the search pattern <regex> is in and <string> is text . The function re.search() returns a match object rather than None. That tells you that it found a match.

re.findall()

This function returns a list containing all matches.

The list contains the matches in the order they are found.

If no matches are found, an empty list is returned.

re.split()

This function returns a list where the string has been split at each match.

In this example, we split at each whitespace character.

re.sub()

This function replaces the matches with the text of your choice.

In this example, we replace every whitespace character with '-'.

Exercise 5.3

  1. Find the first 'a'
    Find the first location of the letter 'a' in the text. Print something like:
    In Text1, the first location of the letter 'a' is in position {position} of the string.


  1. How many 'a'?
    Find the number of 'a's in the text. Print something like:
    Text1 has {number of 'a'} letter 'a' in it.


  1. How many words?
    Find the number of words in the text. Print something like:
    Text1 has {number of words} words.


  1. Replacing 'a' by 'os'.
    Replace all the letter 'a' by 'os' in the text. Then, print out the new text.