The third method used with regular expressions is find all. I think this is by far the most used method, because instead of returning a match object like match and search did, it returns a list where each element is a pattern that was matched inside the target string. Remember that the Find all method, as its name implies, returns a list of all the patterns that were matched. To see this, let's consider the same target string. And let's say we want to match the IP address. So this substring right here, using regular expression syntax, and the Find all method, the IP address we want to match consists of two digits in the first octet, then a dot, then two digits again, then another dot, then two digits again, another dot, and finally a single digit.
Let me write a solution to match this pattern. And then I will explain it in detail. Of course, you should always import the rd module first. Okay, so this looks awfully complicated, but I think it's not. First, I typed in two backslash D special sequences, meaning Python should expect two consecutive digits. Next, I used a backslash dot.
This is called character escaping, because the.in the world of regular expressions, as we've learned in the previous lecture means any character except the newline character. In order to match an actual.in the string the dot character itself, you are required to escape it using a backslash. This also is valid for question marks plus signs parentheses and any other characters that also have a special meaning in regular expressions. Then, instead of using two backslash DS, I used a single backslash D and then number two inside curly braces, which means the previous character any digit 02 line should occur two times only, then another escaped.to represent the actual.in the IP address. For the third octet of the IP address, I used a set of characters as it is called in the regular expressions world, which defines a range of characters or a character class that should be expected at that particular location in the pattern.
In our case, we have this character class doubled, because we are expecting two digits again, then another escaped dot. And finally, I use the same range of characters zero to nine, which this time is expected to occur between one and three times. This means either one, two or three times. Let's get one thing clear. I could have used just one of these equivalent notations for representing a group of digits, for example, backslash D, backslash D, but first of all, it would have been kind of boring. And secondly, I wanted you to learn several ways in which you can represent a number or a set of consecutive digits inside the pattern.
It never hurts knowing how to solve the same problem in multiple ways. The final result of the final method is going to be a list having a single element the IP address matched by the regular expression. Let's check this a and type of a. So as I previously said, we don't have a match object returned this time. Instead, we have a list. We can also use grouping with the final method.
By enclosing the groups we want to extract in between parentheses. Let's consider the same initial string and almost the same pattern as an argument to the final method. This time grouping each IP address octet inside parentheses. Okay, so this is it. Also be careful to leave the escaped dots between the octets outside the parentheses. Now, let's hit enter and let's see the result.
This time Python returns a list of topples as you can See right here, where each topple element is a group from within the pattern. So we have one group, two groups, three groups, and the fourth group right here. And we also have four elements inside this topple. If we would have had two IP addresses inside the initial string, then we would have ended up with a list containing two topples. Actually, let's check this by randomly inserting another IP address in the initial string. So let me redefine this.
And let's add For example, 10 dot 10 dot 10 dot 10 another IP address. Okay, now let's try this just for fun. Let's use the Find all method again to find all the matching patterns inside the string, Enter. And now let's see a and indeed this time we got a list of two tuples as expected. Lastly, let's talk about the fourth method you should know about when using regular expressions and that is the sub method. This method simply replaces all occurrences of the specified pattern in the target string with another string that you provide as an argument.
Let's see this in practice, let's consider the same initial string and replace all the digits inside the string with seven. For this, we should apply the sub method by specifying the pattern to be replaced as the first argument, and then the character replacing each occurrence of the pattern as the second argument, and finally, the variable pointing to the original string as the third argument, so let's check this out. We have b equals r e dot sub, the pattern the character to be inserted instead of the pattern and the variable pointing to the string. Let's hit enter and let's see B, you can see that each digit that was matched by the backslash D special sequence was replaced with seven. Okay, great. I know that the regular expressions topic is a hard topic to digest at first to better understand it and also Learn additional ways of matching patterns.
My only advice is to get into the Python interpreter and start playing around with patterns and specific methods. Sadly, there is no better way of grasping and mastering this topic than to practice as much as you can.