[00:00] Today we are going to have a look at regular expressions We are going to look at matching how to do captures Regular expressions are used for a lot of things, such as grabbing [00:15] a bit of text from a string, validating that a string is in the right format Now I'm going to be using a really useful website called regexr if you're interested and want to follow along. [00:29] I've been searching for Cat. but obviously that's not what makes it powerful. you can use here to help you with your searches. [00:42] anything that starts with a letter or a number. So with \w, it will match every alphanumeric character [00:55] as well as things where we've got a three here as well is matching. So here we can see we've just got this 3at thats matching at the bottom. [01:08] So this will match spaces as well as tabs and new lines. So we haven't got anything here that starts with a space and an at. So here we can see we're matching each of the spaces with an a [01:27] and it will also matching whether a is preceded by a new line. Now these three W, D and S also have the negative versions as well. So if you wanted to search for everything, that's not a letter or a number, [01:39] So here we can see it's matching just the spaces. Or we can do not a space with a capital S You can see here that that at here with the number isn't matching. [01:53] that is used to match any character in your string, and that is a dot. we can see we're matching everything apart from new lines here. one character followed by dots, but say you don't care [02:07] how many characters you have beforehand, you just want everything ending in an a. So here we can do a .* which matches zero or more characters. There's also a + which matches at least one character. [02:19] So you see here is very similar but it’s matching at least one. So if we had A on a new line, it would also match that as well. So here we can see [02:33] we're matching three a’s because we've got this question mark here. we can use a curly brace in here, If we had in a few more a’s in here. [02:48] So we've got another four here and then another five. If you wanted to match all of these, then you can also do a comma five, to match everything where there's an a in the range from 3 to 5 of them. [03:03] In regex it's also possible to put in a range of characters to match for. we can see here we're matching on cat, sat, mat but say we only want to match it if it starts with a C or an S Here [03:17] we can put in square brackets this time and C and S and then close bracket. You can also include ranges as well. Say we only want to match from things that range from letters A to P, [03:30] you can see here that we are also matching cat, mat but we're not matching on sat. So say we don't want to match anything and therefore we only match on everything that's not in A - P. [03:45] But what if you want to extract So let's say we're interested in email addresses, but we only want the domain of those email addresses. [03:59] So in this case, I'm just going to do a \w to find everything before my @ symbol. for email addresses that caters for every single type of email address. So this is where we put a star in, and then we want an @ symbol to say [04:16] Then we're going to do another \w with a star to say, give us everything after the symbol and then we're going to do a dot. we actually want an actual dot, so we need to escape it with a backslash [04:31] and then we do another \w to end in our dot com with a star. So here you can see it has matched that email address nicely. So to do this we add in brackets around the bit [04:46] If we go to details on here, we can see that we have a new group and the group matches our domain names. So let's say we're going to split out the domain extension [05:01] And then another one after the dot to match that as well. one for the name of the domain and the other one for the extension. [05:13] If you need to match a load of file names, You can do that with regular expressions. that people find particularly confusing is positive and negative [05:26] Whenever you want to match something that's when you use these positive and negative look aheads. So here we're matching on every single a. [05:40] Say you only want an A if it's followed by a T here, you can put in bracket followed by question mark then equals T, and that gives us a positive look ahead. But let's say we want the opposite. [05:54] This is what we call a negative look ahead. And to do that, we just replace that equal sign with an exclamation mark. all of the occurrences where they have an a isn't followed by t. [06:07] but instead we're just looking backwards instead of forwards. So here we want to see all of the A's preceded by a space So to do this we do open bracket question mark again, [06:21] but this time we need to add in an arrow to say we're looking backwards. Then equals and then we'll do our \s for our new line character. so we've got an eight here because there's a space before it. [06:33] but we're not matching any A's that are in the middle of a word. look behind, you just need to replace that equals with an exclamation mark. [06:45] And now you can see here we've got all the A's, all preceded by a space. So it's possible to do multiple searches in one regular expression. preceded by an S as well as all the T’s. [06:58] and then add in a T and you can see here we're also matching the T’s. if you're going to be chaining on lots of different expressions. then I recommend that you split these out [07:13] If you like this one, then you might like this one on Stack and Heap Memory. Thank you for watching and I'll see you in the next video.