Regular Expressions (Regex) Mini Bootcamp

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
everyone my name is Colt and this is my little mini course maybe not so mini course on regular expressions really important topic I get asked about it all the time and it's great because it's not really language specific at all it's its own topic that anyone can learn and then apply to Python or JavaScript or Java or whatever language in other news I did release a react course relatively recently and if you want to take a look at that you can find a coupon in the description for significant udemy discounts as always alright let's get started so the first thing I'll show you are the notes or the handout I made for this video you can find the link in the description I highly recommend you check it out it includes exercises fancy exercises actually you can toggle the answer to show and hide some tables a bunch of cheat sheet references and images and all that stuff so let's get started with regular expressions so the basic idea of a regular expression let me zoom in a bit or regex as a lot of people say and what I'll probably resort to saying in this video the whole point of them is to help you match patterns within text or strings and what's nice is that regular expressions the syntax is almost uniform across all programming languages so regex is sort of its own topic that exists on its own and then is implemented across different languages so I'm gonna show how to how to use them within Python and JavaScript but you can also use them with pretty much every commonly used programming language out there so here's a simple problem to illustrate why you would want to use a regular expression imagine I don't know we have a huge dump of text data text message data from our boyfriend or girlfriend's phone and we want to go through and find every reference to a time so something like this let's meet at 11 : 45 or I ran a 3:30 marathon so that seems relatively simple but if you think about how you would do that just using sort of string methods and looping iteration conditionals it gets pretty complex because even with just time it's not as simple as to number and then a colon and then two numbers those can't be any two numbers we can't have something greater than 60 over here and in this case we can actually have one number like one forty five or two numbers 11:45 but we can't have thirty five or sixty or some larger number so we have to be careful we have to make sure that we're matching a pattern with a valid time in addition what if we wanted to optionally check for seconds so this could be valid but we also want to allow for seconds they don't have to be there but if they are we want to recognize it this is a very simple example but trust me this would be very difficult and painful to implement without regular expressions lots of very messy nested logic so regular expressions help us define patterns like this that we can then find just defined or test if they exist but also we can extract them we can use regular expressions to help with searches to replace or update text there's a lot of things we can do just as an example of some really common formats that you can use regular regular expressions to help you with email addresses phone numbers there are many ways of writing phone numbers and this doesn't even account for country codes but sometimes you put parens around the area code in the US sometimes you use dashes sometimes spaces but this is also valid as a phone number so we want to allow for all of those or passwords we want at least six characters one or more uppercase letter one or more lowercase one or more number and one or more special character so that's kind of difficult and it's possible but it's a huge pain to do on your own without regular expressions alright so we're going to dive into the actual syntax and learn this stuff the first thing I want to point out is the link I included here next to this light bulb call-out this is one of many online editors for regular expressions it's called reg X or our reg XR I think it's reg X er anyway it's a live edge etre where you can type the regular expressions up here we'll come back to the syntax and then have some text down here that will be matched hopefully if you're doing it correctly and it will show you the matches as you type them so there are many of these tools there but I highly recommend you use it or use one of them because it's really easy just to make a mistake and you want to see sort of a live result of what you're typing so the next thing we need to talk about is the basic syntax when we write regular expressions we usually wrap them in these forward slashes so these don't actually mean anything you can think of it as like the quotation marks in a string they are not really part of the string content they are there to just denote this is a string so I'm actually going to leave off those slashes in my notes like you'll see some of these regular expressions here there are no forward slashes surrounding it and the reason I did that is so that you can copy and paste them easily into reg excerpt because if i zoom in here you can see there are slashes there I can't delete them so whatever I type in here goes inside of those slashes automatically okay so our very first simple regular expression is not going to be very useful it's just the letter Z so if we type the letter Z in a regular expression all that it will match the only pattern it's looking for is an instance of the letter Z so let's type a Z right here and let's put some text in here like this sentence zebras say Z when they are dazed and you can see we have some matches highlighted the letter Z and then three other letters eese right here now these are all separate matches so this is not one match these are three and this is the fourth also notice the capital Z was none at all matched if I change this to capital Z we now have one match if instead I type a you can see what we get there if I do something like a quotation mark or matching those if I do a chi we have two matches so this is one and that's the second and I should note that you can't directly match every single character just as is some characters have special meanings for example a period or a dollar sign and so we'll learn in just a while not quite just a moment but later in this video we'll learn how to actually match a dollar sign instead of having a dollar sign mean as something special how to escape it but first we're going to take a look at a chunk of syntax these symbols in regular expressions called character codes so there's a table here with a bunch of examples let's start with the first one so if we use square brackets and put some letters or numbers or something inside of those square brackets it's going to match any of the included characters so if we come back to this example if I did uppercase Z lowercase Z inside of those square brackets we now have you can actually zoom out here and you can see five matches so we have the four lowercase disease being matched and then one uppercase Z I can also you know do something like this AEIOU and I'm now matching all the vowels each one is an individual match we can also do the opposite we can put this carrot in front to negate that so I can match anything that is not a foul by adding that in right there AEIOU there we go and if I wanted to expand on this I could add in a second example let's do a e i o u so this is going to match a non vowel followed immediately by a vowel so we get that right here ze RA si and so on so each one of these pairs is a match although you can also see we got this right here a space followed by a all that we said is not a vowel we didn't actually specify it has to be a letter we just said not a vowel and also we only specified not a lowercase vowel so if we had an uppercase vowel like this right here it no longer matches alright the next set of characters is sort of a variation where we can use a range if you can see that a to Z will match any character within that range but we're not limited just a to Z but I can show you that here if we did a to Z like that we end up with a whole bunch of matches notice it is still case sensitive I can do uppercase a to uppercase C and that will match any uppercase letters I can also do uppercase a to Z lowercase a to Z I could do numbers I could do 1 to 5 I don't have any numbers but if I added them in here one two or one three six seven one and two are matching so we can do a range you can do ranges like I don't know let's do f2p and that will match letters that fall in that range next we have the period the dot so this will match any character except for line breaks so remember I said a period has a special meaning if we were actually trying to match a period this will match and but it's matching everything else as well every single character in here is matched but if I added a line break you can see that line break is not matched okay next couple quick ones we have /w this will match any word character so a word character is an uppercase letter a lowercase letter or a number zero through nine a digit or an underscore so if I show you that right here if we say word character and then we follow it with a second word character this is going to actually let's do a couple more let's do five six of them this is going to match six character words we only have one of them if we ignore this gibberish so one two three four five six these don't match because they are shorter a word character does not include a space but remember this also includes underscores so if I did something like this that is now a match because I put an underscore in there it's not a letter it's not a number but it still counts as a word character same thing with numbers that put them right there and this matches as well so that's backslash W we have backslash D to get a numeric digit so any numeric digit looks like this just a single digit so if I was trying to match three digit numbers I can do this digit digit digit and now we're finding three digit numbers now I could have written that like this one or zero through nine and then done the same thing again zero through nine and there's actually an even easier way of repeating something we'll see in a moment but we're doing the same thing here and then finally we have backslash s which will match any white space so if I come back here if I do backslash s you can see one match another match and so on if I had more spaces like right here those are all individual matches so right now I'm getting 14 matches so that's backslash s all right so it's now time for an exercise here it is given this regular expression right here I hope you don't just copy and paste this and test it just try to do it how many matches are going to be found in this string and if you're doing this on your own I recommend pausing it if you need to try and figure it out and the answer is we are going to find five matches what this regular expression does is it looks for any numeric digit zero through nine immediately followed by a one three five seven or nine so this is going to give us two digit odd numbers so we have how many of those we have five 11 13 15 17 and 19 and here it is in the editor can see 11 13 15 17 19 and 5 matches okay so that was the first exercise next up we have another group of special characters in a regular expression called what they don't really have a name but I'm calling them quantifiers and more so these help you specify a quantity of a character or of a sequence so the first one is the plus sign the plus sign will match one or more of whatever came before it so in this example we have a plus uppercase a and it is going to match anything where we have a single a or Moraes so let's do something like this H followed by a lowercase a and then one or more lowercase a s and that will match ha but it's also going to match haha excuse me ha and so on now it won't match uppercase cause just lowercase and it's only going to match one H if I added H plus a plus now I can do something like that so that's the plus sign next we have star so star will match zero or more of the previous token so you can see here I have a color regular expression where the U has a star after it so this means it will match zero use one two five a million use color is a match color with a u as a match in color with three use is a match so if I came back here and I change this to a star we're now also going to match just ages because the a doesn't have to be there it can be there or there could be ten A's so let's change it to an H at the end like this H star and that means will now match ha like this like this and so on a lot of different ways of laughing next up we've got curly braces so if you specify a number inside of curly braces it will match a specific quantity of whatever came before it so let's well we can keep this for now delete this and if I type lowercase a and then five this is going to match anywhere where we have five lowercase a s in a row so I could have also just done a a like that and that's the same thing but we're not limited to single characters I could do something like any digit we want three of them so then if I start typing numbers I don't know one two one two three one two three four something like that any three digits in a row are matched so that's the first version of the curly braces but we can also specify a range so if we pass in two numbers like one and three over here we will match whatever came before at a letter A or a digit will match between one and three of them so if I come back over here let's say let's type a phone number let's see two one two four five three nine nine nine nine so if I wanted to match each individual chunk of the phone number I could say any number any digit three or four in a row so now we have a group of three a group of three and a group of four so it is inclusive so whatever number you put at the end there will be matched we put four we got four in a row right here we can also do a number and then a comma which will match the specified number or greater so if I had something like this and I wanted to match exclamation points and I wanted there to be three or more I could do exclamation point braces three comma and this just matches three or greater exclamation points as you can see here we have one match and here we have a second match this one did not match next up we have the question mark now a question mark makes something optional it means that we can match one or zero so if we use this color example so we have color color with a u and then color with a ton of use if we start a regular expression so we want it to match color so Co ello and then an R at the end but the U is optional if I just type this we're only matching color directly if I use a star as we've seen after the U we match all three but if I only want these two I can change this to be a question mark and that just says zero use or one u so this is not in the running anymore and lastly we have the pipe you've probably seen this before if you've done much coding it's similar to the boolean or operator so when we use the or here it matches whatever is to the left or to the right of the pipe character so here I have some text puppy puppies poppy poppy Eppie yuppies so all of them end with either p py or PP IES so if I wanted to match them the entire word all of these words first they all begin with some letter or multiple letters it could be a y it could be a P so I'm going to specify here some number of word characters now that does match all of them but that would also match something like dogs which maybe I don't want to match I only want words had end with py or piee s the plural version so some number of word characters and then they all end with two P so they don't end but they have two peas before the ending and then we're going to add parenthesis and say either a Y or IES now the parentheses actually have an additional meaning that we'll see later in the course in this video don't worry about it for now just know that they actually do something in addition to what we see here but for now they just allow us to make a decision for the Reg of the regular expression to choose a Y or an IES if I get rid of those parens it's a bit different because now we're doing this entire thing or this IES so you can see we're matching puppy and we're matching IES but if I add those parens back in now it's doing what we want all right so it's time for another exercise given this regular expression oh it's not a zero it's a lowercase o question mark K plus how many matches are found in this string you might want to reference this here the charts I wouldn't expect you to just remember all the syntax right now but given this string how many matches are found all right pause it and here's the answer we get four matches so the pattern is that we're looking for an optional oh so one or zero O's and then any number of K's afterwards so okay is a match 100 and 1k k k is a match we don't need the oh it's optional k is a match and okay as a match as well all right next we're going to talk about a group called anchors so this is very short group two things the carrot and the dollar sign so what these do is allow us to specify where in the texts the regular expression should matter where the pattern should be found so if I come back here let's say that I want the word that we're matching or this pattern to only come at the very start of our text I can add the carrot at the very beginning and this basically signifies the beginning of the string or the beginning of a line so none of these are matched because yes they might qualify for the rest of the pattern but they don't actually come at the beginning of our string so if I had I love puppies it doesn't work if instead I change that to add a dollar sign at the end the dollar sign means end of the string or end of the line as you can see here you can hover matches the end of the string now we get a match and then the other thing that I can do is combine them together like this and this says basically the entire string has to be a match we have to have the beginning immediately followed by our pattern immediately followed by the end so now we could test is this if we were using JavaScript or Python or some language we could check is this entire string a match or if we didn't want that we just wanted to know does the whole string contain puppies somewhere inside of it we wouldn't use those anchor characters but if we want to make sure it's an exact match of the pattern and nothing else then we can use the anchor characters okay so next up another exercise given this regular expression should say given I'll fix that hopefully which of the following four choices contain a valid match so pause the video take a look this combines a lot of what we've seen so far all right so what we have here is the beginning of the line anchor and then an uppercase letter we have a range so any uppercase letter two times so any two uppercase letters then a space character so that could be a space or a tab something else and then five number digits five single digits in a row that's what backslash D is so this means to uppercase letters a space five numbers and if we take a look at our examples the first one does not match that because it has to come at the beginning of the string so this has the pattern but it's not at the right place this one does not match because there's no space we required a space this one does match it comes at the beginning of the string is also just the entire string and this one matches as well at the beginning as you can see here this is not a match because we put the anchor character at the start next up we'll quickly talk about escape characters so as we've seen a lot of characters have special meanings in regular expressions the plus sign question mark all of these I've listed here the star the carrots are the period brackets and if you needed to actually match them in your code or in a regular expression you need to escape them using a backslash so you can see this regex here I'm matching a digit and then a plus sign so if I actually just put the plus sign without the backslash then it has its own meaning the plus means one or more or one or as many as you want but if I want to actually match a plus sign I need to use backslash Plus so if I wanted to match dollar amounts like 1050 or 9999 and have a dollar sign in front let's say some of them have a dollar sign let's do 425 and I want to match all of these what I could do if we delete this we're going to have an optional dollar sign so to type the dollar sign its backslash dollar sign with a question mark that makes it optional and then we have one or more digits so that's a plus sign because we might want a price of I don't know for tu-tu-tu-tu-tu like that then we're going to match a period which will need a backslash for because a period on its own means something else and after that we want exactly two digits backslash d2 so we could make it match a whole number like 50 instead of 50 point zero zero but we won't worry about that for now so if we didn't have the backslash dollar sign and the backslash period it means something entirely different so we do need to escape those all right one more exercise here this one I'm asking you to write something so write a regular expression that matches an exponential expression like this four to the second with a carets 12 2 v 3 to the 1000th 120 seconds and it should also work with negative numbers so 5 to the negative 3 or negative 15 to the sixth so it should be basically any number down here and then a carrot and then any number afterwards and it should also work with negative numbers so give it a shot pause the video and here's the solution so there's more than one way of doing this here's what I wrote it looks like this let's bring it over here I'm checking first for an optional negative sign with a question mark and then one or more digits and then we have to escape the carrot so let's do escape the carrot that sounds like my childhood at dinner I do not like Cara's okay so here's what we have negative sign is optional let's put one on this one and then one or more digits then a carrot that must be there and then an optional negative sign and then one or more digits now you could have also added in the anchors to see if a string only contains a single exponential expression - matching multiple in a given string but if you add the actual carrot in not the escaped version and a dollar sign then it makes sure that the string is only a match and that there's no excess information or data next up we have one more very important feature a little bit of syntax something called capture groups so when I said earlier that parentheses also do something something in addition to what I showed you this is what I was talking about so if we return to this example I'm gonna paste it in this was a regular expression that matches to uppercase letters it's supposed to be a state code in the US a state abbreviation and then some zip code which is we're not actually checking is this a valid state right there's no state that is zs but this still matches and there may or may not be a zip code two nine eight thirty seven but we're matching the pattern and right now when I look at the match we're just matching the whole thing which makes sense is how it's been working so far but if I was writing code where I wanted to extract the state and extract the zip code maybe I wanted to figure out which state was making the most orders so I went through all the addresses I extract the state from every single one and then I do some math to do that I mean we could just take the match and then take the first two characters and do it with JavaScript or some language but we can also separate them out using parentheses using a capture group so I wrap whatever I'm looking for whatever I want to isolate it's still part of the pattern but now if I look I have a group you can see group number one at the bottom is ca or this one is zs let's change it to a valid state like New York and then I can wrap the same thing here around the zip code portion I don't want to include the space now I have Group one New York Group two is zip code so I've separated them out but they still both have to be there this is not a valid match if I just do CA so this is very very useful as we'll see in just a moment when we talk about writing regular expressions and using them inside of a programming language often you're not just checking to see if a pattern is there but you want to do something with it you want to replace one part of it and keep the other parts the same maybe you have a huge data set and you want to go through and take all the capital text and change it to lowercase text or you want to find all the addresses and you want to like I said extract the state or you want to read all the email addresses in a file so you might have something like this and you want to match all the emails so this could be a valid email as well and you want to hide the first part so you want to find all of the emails but then you want to replace the part before the @ sign with dashes or something or anonymous you could use regular expressions with capture groups and you would put a capture group around the name here like James and you would remove that but you still need to make sure you're matching this whole thing because you have to know the context we don't want to just replace any word we want to replace the word that comes right before a whole email address so that's where capture groups come in handy so we're going to switch gears a little bit we're going to write a larger regular expression nothing crazy but a little longer which we'll use to help us find times a 24-hour clock time format so all of the following should match our pattern we actually talked about this at the very beginning something simple like two 45 or 245 with a leading 0 or 23 23 50 and seconds so it's not as simple as two numbers a colon and two numbers it could be one number a colon and two numbers it could be you know hours minutes seconds and we also want to make sure that we're only using valid times so 2580 should not work there's no 80 minutes there's no 25th hour so if you want to try this on your own go ahead pause the video otherwise I'm going to show you my version and there's more than one way of doing this I'm honestly not great at writing regular expressions I'm going to talk about this at the end I usually recommend you just fine pre-written ones by actual experts and people who know if they're doing unless you're trying to match something that is very very unique alright so here's my initial version let me just quickly walk through how it works we have a couple different sections the first bit is going to match a 0 or a 1 as the first digit or a 0 through nine now if it doesn't start with a zero or one we have the or which means it has to start with a two we can't have 30 something at a time we can have zero zero zero one zero nine or we can have one zero one nine but then once we hit two once we get to 20-something o'clock we can only have two followed by zero one two or three we have no 25 or 26 o'clock so after that first group then we have a colon then we have our sec or our minutes excuse me so the minutes must start with a zero one two three or four or five and then be followed by any digit so we can't have 70 or 80 seconds or minutes we have to have somewhere between zero and 59 and then optionally this whole thing is optional at the end so if the question mark means we have a colon and then the same exact pattern 0 through 5 followed by a 0 through 9 and so this is all working as you can see here we have a match this matches we can do 930 the problem we have right now as if I try and do something that is invalid like 89 43 we're still getting a match so what we could do that we've seen already is use an anchor to say this must come at the beginning of a string but that might not be what we want what if we want to match this it is 245 well there's one other special character I haven't shown you which is backslash B and this is called a word boundary character it matches let's scroll over it matches a word boundary position between a word character and an on word character so you can see now this doesn't match because earlier this was a match but now we said it has to start with a word boundary which means there must be a boundary here it can't just be another letter it can't be something like that or a number it must be the start of a line or a space all right so I'm going to add that in and we'll revisit the capture groups right now we have one group which is around the hours portion so this is a group and then the colon is not in a and then we don't have a group here so we could add parentis right there so now when we hover we have group one is 23 group two is 50 but the problem is group 3 group 3 is this whole thing which we need those parens to make it all optional and we need the colon to be optional if we move the colon here we now match things like this but we would also match this as a time which we don't want so we need to say that the colon is optional but we want a way to isolate this information right here one option is probably I think this will work just put more parens in there but now we end up with four groups you can see we have Group one group two and then Group three is : 32 and group 4 is 32 well we have one more option which is something called a non capturing group now this is going to look quite weird but if we go in here at the beginning of this group and we add a question mark and then a colon what this means is that these parens this whole outer group should not actually function as a capturing group we just want those parens there to make the whole thing optional we were trying to apply the question mark to all of this and so the question mark colon means it's an optional group or a non excuse me it's a non capturing group and then we need the second colon here to actually add the colon into the regular expression and now if we take a look we have three groups there's no colon in the expression or in the group we still have to match the colon but it's not being captured then there we go this is working alright so this is a simple enough example I'm sure someone will find an issue with it which is great there's at least one issue I've found so far with this regular expression and this is kind of what makes regular expression it's difficult is accounting for all the edge cases making sure you don't get any false positives just fully testing them and let me show you what I'm talking about here if I have a valid time like 12 45 36 this is matching of course if there's a letter at the beginning or another number at the begin it doesn't match but if there's an extra number at the end this is still matching or we could have this format and this is not a valid time the entire string here or this entire chunk but we're matching this part so you might think you could fix this if we add a word boundary at the end but as you can see here it's still matching this first part also if I added an extra numbers now we're only matching this part this is considered a word boundary in between those characters so if we're up for it try writing a better regular expression here that won't have these false positives where we have numbers afterwards or if we have extra colons this is not valid but it's still matching and also I'm curious to see if anyone else finds other issues I'm sure there are some other problems that always happens to me with regular expressions next we're going to talk about how we use these in JavaScript and Python so as I mentioned earlier pretty much every programming language out there has support for regular expressions built-in out-of-the-box sometimes you might need to import a module like in the case of Python and the method names might vary between languages but the actual patterns you write the syntax the characters for the regular expression are very very similar if not identical so you learn one syntax for the expressions and you can apply it in all sorts of languages so we're going to start with JavaScript JavaScript comes with a good amount you might say a healthy amount of regular expression methods here they are exact tests match match all search replace split they all do some of them actually do the same thing but most of them do something different we're just going to focus on the first two exec and test but before we even see those methods you need to know how you actually write a regular expression in JavaScript how we tell JavaScript hey this is a regex and it's nice and easy we just write slashes so if I wanted to make a regular expression variable I already have this on my clipboard this is non valid JavaScript just to have a plain old regular expression like this first I'm going to make a variable this is our time regular expression but it's still not valid I could wrap it in quotes but the easiest thing to do is to wrap it in slashes so on either side I put a slash and one quick note I don't have time to go into this you'll often see AG there it's called the global modifier if you are a JavaScript developer I highly recommend you look into that there are some weird quirks the way it's implemented it's pretty bizarre but for now this is what this is it just slashes we now have a valid regular expression and we can start by using one of our methods so if we look at the methods we have available they're going to show up here I might need to go down into the console if I come down here time regular expression here we go we have a couple different methods the one we're going to focus on first is called test and what test does it's a boolean method and it accepts a string and it will return true or false that's it it won't tell us what the match was it doesn't give us any information other than if the string contained a match now this is useful if that's all you need to know because it's a lighter weight method it's faster if you don't actually need to know the match information you don't need to know you know what time was matched what are the hours what are the minutes what are the groups if you just need a boolean this is easy so we can put a time in here like 145 we get true if I do something like let's do 99 45 we get false if I do let's do 9 45 with 45 seconds we get true so that's pretty much it for tests you could write some logic right you could do if our time regular expression dot test some variable some user input something in our database then you can do something else do something else but next I'm going to show you exec so exec is a bit different it actually returns the information the matches that it finds so if we do our time regular expression dot exec we pass in something with no match it's easy we get no but if there is a match if we did four let's do what about 1240 26 it returns an array and this array has a couple of things in a very particular order the first item is the entire match the second item is the first group the second group and then our final group the third group for seconds so we could now extract the information we need out if we needed the whole match we would access the zero with item but if I wanted to have the minutes of the hours minutes and seconds separated out I now have them right here so that's a very very basics of regular expressions in JavaScript next let's take a look at Python so python is a little bit different in that you actually have to include a module it's a built-in module you don't have to install it yourself it comes with Python but you do need to actually require it or import it it's called re4 regular expression and it has a bunch of methods that it includes I'm just going to show a couple of them I really just one which is called search but first I need to show you how you write a regular expression in Python so we can't just use the back slider the forward slashes we actually need to use a string and you could do a regular Python string but then that's a pain because some of these characters like a backslash is a special a spate escape character in a Python string and that gets very annoying to have to double escape things or triple escape them sometimes so instead we can use what's called an R string it's just R and then a single quote and then our regular expression and then the end of the quote and this just tells python all right don't touch the string I want it as it is then once we've imported our e we can run our e dot search and pass in our regular expression and then a test string alright so I open the Python 3 repple I ran my code well I imported regular expression re I set up my time regular expression variable and a test string now I can actually test it I can do our e and the method I'm going to show you is search there are many other methods included with our e but search takes the regular expression pattern first and then something to test it against so I just did it on separate lines or separate variables to make this shorter and what it returns is something called a match object so this is different this is different than JavaScript you know the values that are returns the name of the method what this match object contains is match information and I can access it go back to the notes by calling a method called group so I need to save it to a variable so let's do that now let's call it res equals and then I can call resdac group and if I don't pass in a number it gives me the entire match so I passed in the string it is for 30 47 if I do res dot group with no prayer with no value just empty parens I get the whole match and the same thing if I do zero as you can see here but if I pass in one it gives me the first group two it's the second group and three is the third group so hours minutes and seconds so another way of retrieving the group information the capture group information a slightly different style between JavaScript and Python definitely I prefer the Python version the point is that we can use our same regex in JavaScript or Python we can do the same things we can get the groups it's just different syntax all right so that's it for now I know there's a lot that we covered here it's pretty crazy it's a big topic the last couple things I want to touch on our topics I did not cover so this was relatively comprehensive but there's still some other topics and important syntax I did not talk about with regular expressions if you want to take a look first numeric references I didn't talk about that positive and negative look around substitutions and flags so there's probably more but these are for the more important topics I did not have time to cover lastly I want to end on this note if you are trying to use a regular expression to do something common like matching an email or phone number and address credit card numbers I highly recommend you do not try and write that yourself definitely go online and find an existing version there are many many blog posts and websites and resources where people can share regular expressions comment critique upvote that sort of thing you don't want to have to create something very complex to match let's say a URL and you don't want to have to rely on your own skills because it's really easy to mess up so if you go to regex or comm there's actually community community patterns if we look at one like let's say an email if I can find email if I can search here here we go here's an email regular expression now typing this yourself is really annoying and it's not even a matter of typing it it's the knowing what is actually valid in an email making sure you allow for all possibilities that are valid so if I do Colt at yahoo.com that matches I can do colt dot cats at gmail.com that matches so anyway the point is don't try and write these on your own unless you're doing something that is relatively unique or simple I highly recommend you just try and find an existing one all right thanks for watching I hope you enjoyed it take a look at the notes let me know if you have any questions or feedback and yeah do the whole like comment subscribe thing if you have time for that I appreciate it alright bye
Info
Channel: Colt Steele
Views: 13,977
Rating: 4.9906869 out of 5
Keywords: regex, js, python, regular expressions, code, colt, steele
Id: EiRGUNrz9MY
Channel Id: undefined
Length: 46min 2sec (2762 seconds)
Published: Thu Jul 11 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.