In the previous lessons, we saw how we can match letters, words, and metacharacters. In this notebook, we will see how we can use regular expressions to perform more complex pattern matching using metacharacters. The first metacharacter we’re going to look at is the backslash. We already saw that the backslash can be used to escape all the metacharacters. However, the backslash can also be followed by various characters to signal various spatial sequences. Here’s a list of the special sequences we’re going to look at in this notebook. So, let’s start by looking at the special sequence backslash d. This sequence can match any decimal digit; that means it can find any number from zero to nine. Let’s see how this works. Let’s suppose we wanted to find all the numbers in this sentence. To do this, we will just use the backslash d as a regular expression. If we run this code, we can see that all the matches found by the finditer method correspond to decimal digits between zero and nine. Conversely, if we wanted to find all the characters in a sentence that are not decimal digits, then we will use a regular expression backslash capital D. The backslash capital D will match any character that is not a number. So if we run this code, we can see that none of the matches are decimal digits. We can also see that the backslash capital D is able to match all characters, including uppercase and lowercase letters as well as whitespace and the period. Now let’s take a look at this special sequence backslash s. This sequence can match any whitespace character. This includes spaces, new lines, and tabs among others. So, let’s see how this works. Let’s suppose we wanted to find all the whitespace characters in this multiline string. To do this, we will use the backslash s as a regular expression. If we run this code, we can see that all the matches correspond only to whitespace characters, such as new lines, tabs, whitespace, form feeds, carriage returns, and vertical tabs. Conversely, if we wanted to find all the characters in a multiline string that are not whitespace characters, we will use backslash capital S instead. The backslash capital S will match any character that is not a whitespace character. So if we run this code, we can see that none of the batches correspond to whitespace characters. We can also see that the backslash capital S can match all characters, including periods, numbers, uppercase, and lowercase letters. Now let’s take a look at the special sequence backslash w. This sequence can match any alphanumeric character and the underscore. This includes all uppercase and lowercase letters as well as the numbers zero to nine. So, let’s see how this works. Let’s suppose we wanted to find all the alphanumeric characters in this multiline string. To do this, we will use backslash w as a regular expression. If we run this code, we can see that all the matches correspond to alphanumeric characters, including the underscore in the email address. Conversely, if we wanted to find all the characters in a multiline string that are not alphanumeric characters, we will use backslash capital W instead. The backslash capital W will match any character that is not an alphanumeric character. So, we run this code, we can see that none of the matches are letters, or numbers, or underscores. We also see that backslash capital W is able to match all whitespace characters, like we see here, and even the @ symbol in the email address.