Repetitions
1. Repetitions
In this video, we are going to talk about how to match repeated characters.2. Repeated characters
Let's imagine that we are given the task to validate a password.3. Repeated characters
It should contain eight characters4. Repeated characters
followed by four numbers.5. Repeated characters
To search for a pattern, we can use the method dot search. It takes the regex and string. It tells us if there is a match. Let's apply what we have learned until now. We use backslash w eight times to match the first part and backslash d four times to match the last part. So our method finds a match.6. Repeated characters
But this seems cumbersome for longer regex. Instead, we can use quantifiers to save this situation. A quantifier is a metacharacter that specifies how many times a character located to its left needs to be matched. In our example, we specify that backslash w is repeated 8 times and backslash d four times. And we get a match as seen in the output.7. Quantifiers
In the following string, we want to match the dates. We can see that the pattern is one or multiple digits, dash, and again one or multiple digits. The plus metacharacter indicates a character that appears once or more times. Let's construct the regex from simple to complex. We will use dot findall method as you can see in the code.8. Quantifiers
We indicate that the digit, backslash d, should appear once or more times adding the plus quantifier. Then, a dash.9. Quantifiers
And again a digit, backslash d. It should appear once or more times, so again we use the plus quantifier. We get the two matches that we expected as seen in the output.10. Quantifiers
To indicate that a character should appear zero or more times, we can use the star metacharacter. In this example, we have the following string. We want to find all mentions of users, which start with an at. We notice that they could contain or not contain a non-word character in the middle. So, we construct our regex as seen in the code: an at, followed by backslash w plus, backslash capital w with start to indicate a non-word character zero or more times, then backslash w plus. And we get the matches seen in the output.11. Quantifiers
Another helpful quantifier is the question mark. It indicates that a character should appear zero times or once. In the example, we need to find matches for the word color, which has spelling variations. So, our regex is c, o, l, o, u which can appear once or zero times, so we add the question mark after it, then r. With this regex, we get the two matches wanted.12. Quantifiers
Finally, we can use the curly braces to indicate a specific minimum and maximum times. In the example, we want to find all matches for a phone number. We'll use the dot findall method and construct our regex step by step.13. Quantifiers
As we can see, we can have a digit, once or twice, then slash.14. Quantifiers
Then, again a digit, three times. Then, a slash.15. Quantifiers
Then, a digit twice or three times. Then, slash. And finally a digit again. We indicate that this last digit need to appear at least four times leaving the second argument in black. The regex engine returns two matches as expected.16. Quantifiers
It's very important to remember that the quantifier applies only to the character immediately to the left. As an example, in the regex apple plus, the plus applies only to the letter e and not to the entire word.17. Let's practice!
It's time for you to put quantifiers into practice!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.