Advanced Date Matching with Regex - Including leap years

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Please don't use regexes to validate dates.

First, regexes are for matching text patterns, not checking numeric values of things. The things shown in this video are clever, but are terrible code. It's fine to find things that look like dates, but for actually validating things like days and months, a regex is not the right tool for the job.

Second, whatever language you are using already has date validation code that someone else has already written, tested and debugged. There's no need for you to write your own code to validate dates.

👍︎︎ 18 👤︎︎ u/petdance 📅︎︎ Jul 21 2020 🗫︎ replies

Probably a terrible idea to do something like this in practice, but it offers some very interesting techniques and practices in regex. It's hilariously brilliant.

👍︎︎ 5 👤︎︎ u/jordonbiondo 📅︎︎ Jul 21 2020 🗫︎ replies

Well done, thanks for the video. A fine example and explanation of what is possible using regexp that includes testing scenarios.

👍︎︎ 2 👤︎︎ u/don_bski 📅︎︎ Jul 21 2020 🗫︎ replies

I work with regex most daily. There are many clever and simple (or not so much) solutions using it. But damn, don't do that for dates. I use it to validate date formats, or reformat them when they're a string, but that validation is just an overly complex format validation. It won't validate a date since it accept wrong values.

I do think there are a lot better examples on where to use that will be useful in a programmer's life.

👍︎︎ 2 👤︎︎ u/ventorim 📅︎︎ Jul 21 2020 🗫︎ replies
Captions
so welcome to this video this video is placed on youtube and it is an exact example of what we are doing in this taming regex course so that if you need to have a regex explained to you or you need regex training please see the description below you should be able to get a coupon voucher for this regex course that we we host on udemy so please reach out to us there or you can put a comment below and we should be able to help you with that too all of these links are in the description below so they're in the order that they come in so every time you see i i change the screen or change a thing you should be able to get the updated one in the description so please follow along with us and we can go from there we have looked at dates in this course before and the dates are quite simple if you're just going to do a basic idea and the basic idea that you've done before is something like this and this first part over here is month and this is an american date and if you do want to flip this around we can obviously grab this whole thing let's cut that and the reason why i'm doing this is because in this example they're about to do we're going to use the uk south african date day first month and then year but obviously if you're using month first and day it's going to be similar to that so let's actually see how this works so we've got days and a day can either start with let's clean up these examples a day can either start with 0-3 and then the 12th month and then let's do the year and you can have the 18th third month and let's say the year 2000 again so that's that is hard work so we've got day here which means that days can be anything from zero to three so if i've got the number 3 there and i say 12 2000 this is still going to match so this 0 to 3 is obviously optional so it's either 0 or 1 of that 0 to 3. so you can either have a single digit one or a multiple digit one in the month we can do either zero one and that is optional as well so either zero or one and goes all the way through to zero from 0 to 9. so now if you have done regular expressions before you will notice that there's a couple of limitations to this so one of the limitations would be that i can say that under days i'm going to have a three and then under numbers i can have a nine and then under months i could have a one and then under here i could have an eight for instance or a nine and then over here we can still have the yo 2000 and that is not a proper date this is really just about like getting your mind around how dates work if you chuck this into i'm just going to copy this into this website and it's not my favorite website because it does i mean if you click on login here it's going to give you an error um if you go to embed on stack overflow it doesn't actually work but what does work very nicely is if you dump your regular expressions in there it doesn't like named ranges so just remove those okay and you say that your number is going to be 0 3 12 2 000. let's take out those question marks for the named range there like that it says cool so that is going to be a match and then it almost gives you a visual description of this and it says one of zero to three one of zero to nine then a slash one of that one of that and then a slash but my favorite parts about this website is this is this position thing so if you grab the slider and you move it what you'll see over here is that that pink line moves down and actually shows you where in your regular expression actually matches so if you move that and you move that it shows you exactly where you are looking and it shows you in the the visualization as well and this is this is quite useful especially when you're trying to break down regular expressions from the internet that you found or if you found something on stack overflow and someone says use this rather than just trying to figure it out yourself dump it in here and you have the ability to do javascript python and pcre it's it's not the best website i mean there's this other website here called regulax or whatever if you dump that in there you can see exactly the same thing and it shows you and visualize it exactly how you would want to have it expressed but doesn't have that little slider that allows you to to grab it across so those are two that are very nice but let me explain what we're actually going to try and do now we are going to try and do a regular expression that looks at every single type of dates and this is my thinking behind it so you can see this obviously as a link certain months have different days so there are 31 days in january march may july and stuff then there are 29 days in 30 days in january march so all of them except for february have 29 and 30 days the third one is that there are 29 days for february in a year divisible by four so that is the year 2000 2004 2020 all of those years are have a leap year in it so that has got 29 days then there are 1 to 28 days for every single other month for every single month so it can either be written as 0 as 1 or 0 1 all the way through to the number 28 for every single month then depending on how you're going to have a separator you can either have a slash or a dot or a dash and theoretically it should be the same as the as the previous separator you shouldn't have one and then a forward slash um o4 and then a dash and then the year 2000 because it just looks weird if you've got a forward slash between the day and the month then you should have a forward slash between the month and the year under months we can either represent them as 0 1 to 1 2 or january february all the way like that so those are three letter representations of the months and then the years we can do years either as y y so just the number zero zero all the way through to four wires so that is the two thousand because if i said the year was zero zero it could either be the actual year zero zero the uh 1000 the 1900 the 2000 you don't actually know but some people would want to represent like that and then what are you looking at in terms of the year do you want to go from the year 0 all the way through to your 999 or what kind of limits do you want to put in place so i think for this one what we're going to do is we're going to sort of like do a birth date validator so let's say that they can be the old the eldest they can be is from 1900 and they can go all the way until 2020 for right now if you need to say that they need to be older than 18 to visit your website then take whatever year that you've got so if we're in 2020 18 years away from that would be 2002 if my maths is correct those are the kind of things that we need to look at and whenever you're doing a regular expression please make sure that you break down your thinking and once you've broken down your thinking let's now put that into a regular expression tester and start visualizing how this whole thing works now what we're going to be looking at is this is my idea for regular expression now if you are a bit confused by this that's fine we're going to go through this step by step if you don't want to have this step by step and you just want to copy and paste it that's fine this is the link for it just grab it from that link and you can copy and paste it into your thing if you'd like to understand how the regular expression works that's what we're going to look at now if you look over here we've got right examples and we've got wrong examples so the right examples are the 1st of january 20 2000 and please remember these are day month year then we've got first of january so we've got one one two thousand instead of o101 and we've also got one and january like that and we could also do remove the zero from there and we've got just the number one in january so all three of those are exactly the same dates but they all are represented differently we've got the 31st of january we've got 31st january we've got dots in between here we've got dashes in between there we've got dashes in between them and then we've got the 28th of of february there 2001. so all of those are cool and we go down there now the wrong ones what you'll notice is this the 31st of april and in april there are only 30 days so that one has broken if you change this to 30 there it works now the 31st of april is wrong the 29th of february 2001 because it's no leap year in 2001 it's going to be wrong and if we change that to 2000 or 2004 or 2008 all of those are perfect but in 2001 it doesn't work the 13th of the 13th month won't work the 30th of february won't work the 30th of some other things so if we type whatever you want in the middle there february let's say that's the 25th of february it won't work so this won't validate if you type the whole of february 32nd of january and the 32nd of january so none of those work so based on this this is my best example of how this regular expression will work i've tried to go through all the possible variations that i could think of in this thing so in day month and year if you can think of anything else that is not working please reach out and we can see if if there's something that i've done wrong but this is the best example so let's try and break this down now and it's very long so let's try and break this down and see if we can actually make this make sense so let's take this example and copy that and i'm going to check it into debugging and paste it in there and this is where this website starts shining so this is where you actually see how it's broken down and you don't even have to understand the regular expressions to see exactly what's happening so this is the regular expression here is the result and this becomes quite difficult so let me see if i can zoom out here a little bit so i've got a little bit more okay so right now the position is at the end and you see that little pink thing there so if i drag this one back you'll see it's a pink thing there and there's nothing else in any of these other lines which means that we're actually in this example we are flowing through this regular expressions to get this pattern so if you want to move it around like that what you'll see is that we are still only on this last one so if i move it back one more step we see that we are there and let's go back a little bit further and you will see over here that that pink line is in front of the 12 there and it is saying we're matching up to there so o3 then a forward slash so it comes from here and then a forward slash which is one of those and then we're over here now so it doesn't know what's happening yet so it's saying we before the 12 we don't know what's happening if you go one step further so we're now inside the 12 we're now the and the one it says it can only be parts of this over here so the one can be a one and the other one can be one of zero or two previously you had these options and previously before it actually starts the 12th it could have been jan it could have been february it could have been everything so it's a nice way to visualize it okay and we're now going to play around with this and we're going to come back to this visualization each time to see if it still makes sense to us so let's break this down and see that this works so i'm starting this regular expression and i'm just putting a full stop in there a period and i'm going to say update this regex so now this next example this next link you'll find and if you want to start from where i am this is how you can start so i'm going to delete the period and let's not try and figure this out so the first thing we have is we have got a zero and a one or a one or a zero and one all the way up until 31. so if we're going to do this we can do a set and we can say the first set is either 0 1 2 or 3 and that is optional while we're doing this let's actually start this from the beginning using a carrot there and see that that actually works so we've got zero one two or three the second one is that we can have the numbers in the set zero all the way to nine okay and that now matches the days of that perfectly but we don't know whether or not this is going to work we don't know if we've got the number 31 or the number 39 if it's actually a good idea so we're going to break this down a little bit differently so under the debug x here we've got three different routes okay so we start over here the first route says if i've got a number either zero which is optional and then one to nine or i've got one as a number and then another number thereafter or i've got two and then one of these then go through this link we're looking at everything from the first day of the month through to the 28th day of the month we're going to do three different things with alternates between those so let's start at the beginning here if we're looking at we can start from 0 1 or 0 2 or 18 or 25 so all of those are perfect numbers so if we say the first numbers are going to be from let's say from zero and then we're going to from zero to nine that will give me 010203 all the way up to zero nine okay then i can say i want to look at all of the numbers in the teens so i want to do it starts with a one and then i can do a subset and go zero to nine close that and then all the t numbers will come up and then i can do last alternate and i can say it's going to start with a 2 and the subset is going to be from 0 to 8. so that will go from 0 1 all the way through to 28. now obviously you could also do things like this you could also say instead of 0 to 9 you could do backslash d and that would be the same thing depending on how confident you are with regular expressions you might want to say well i'd rather represent it like this and i'm going to say 0 to 9 but what you will notice from here is that this over here in the front means that we could also have the date of 0 0 and that's actually not correct so i would like to say that this zero is optional and this actually doesn't start at zero this starts at one so now what we've got is we've got 0102 18 25. so now this is the first leg of this so this is the first leg that says here start at the beginning then either go here so from zero and then one to nine or the second leg starts in the ten and has a digit behind it or zero to nine and and the third one is from two till eight so those are the three parts of that i normally bring out a text editor at this point just so that i have a copy of this because if i lose anything i start getting frustrated and i normally just copy this across and say this is days and i go from 01 all the way to date 28 so that is the first one the second one i'm going to do is i want to do days and i want to do 29 and 30 and the last one i want to do is days 31 okay so we're going to take all of these and put them together just now so in terms of days for 20 29 and 30 we can say 29 or 30 which means that if i've got 28's up here it's not going to work 27 is not going to work one's not going to work 29 is going to work and 30 is going to work so that's perfect let's grab that that's that one and then obviously days 31 is going to be just the days of 31. so now the second part of this here is 0 1 228. this over here is the 29th of february this over here is the 29th and 30th and that's the 31st so if we're looking at this one we can add one more thing to our notepad which is days in feb for a leap year and this is going to be a 29. okay so now the second part of this is we have to have different slashes here okay we have to have whatever slashes we want so the options we have is we are the ones a forward slash which remember needs to be escaped like that so that will make a forward slash work and then if we have a period in between and if we have a dash and if you really wanted to you can add equals you you could add whatever symbol you wanted but i'm just going to leave with those three so that matches those and then if i do an alternate and i said backslash period that's going to match the period there and i'm going to say another alternate and lastly we have a dash so that matches those three things so if i grab this now and put this into my notepad and i'm going to call the separators sep parameters that is the function for my separators the third part is we need to to represent the months okay so the months we have here can either be done as a zero month so zero one which is january or one which is january zero two which is february or two but it can also be represented as jan feb and march okay so let's do that now so the months can be a zero which is optional and then it can be the numbers from one to nine that is going to match o1 two a three okay then we could also have an alternate and that alternate is going to be anything that starts with a one and then the only options for the one are going to be one zero one one and one two or you could do zero two two like that okay and that should match 11 and 12. so now that we've got those we now have solved this part over here we've solved that they can have anything there and we can have all of these we now need to solve that they can be represented as three letters so let's do an alternate now so all of these should now work so if i type jan and i type fib and i'll tap mar and then tap april all of those should now work so i'm going to grab this and this is these are our months perfect then we would have a separator again and the last thing we'd do is years so the question i have for you is what do you want to do as a year do you want to start it from zero do you want to end it at the year 999 do you want to just say we want to go from 1900 all the way to 2020. so whatever you want to do and whatever works for your verifications and validations you need to do it like this i want to say that if they're going to fill in their birth date year that that is what we're going to do now depending on if someone is going to be born in 2020 they're probably not going to be filling out a form on my website so you could say well the youngest person might be 13 filling it out and so then it would be 2007. so whatever you want to do is yours you must do that but let's try and get the thinking behind this first so i would do it from the number one then i'd have a nine there so 1900 and then i would have a range in here of zero to nine and then if you wanted you could do another zero to nine like that and that would match it there so anything inside here would be 1923 would match 1956 all of those would match if you wanted to make this simpler you could also do 0 to 9 curly bracket 2 and that just says we have two of those and if you want to make it even simpler than that backslash d and two of those so whatever you would like to play around with i'm going to do it like this so it's easier just to represent it zero to nine so that you know exactly what i'm doing because i've got zero to eights and zero to whatever's so it's easier for you to notice okay second one i'm going to do is i'm gonna do an alternate chair and i'm going to start with a 20 here and then i'm going to do the same thing i'm going to say what is the highest number that i can possibly have well i can go from 2000 so that's double zero and i can go all the way up to 2020. i can then say the range of this for the first one can be from zero to two so either the year 2000 the year 2010 or the year 2020 and the second one i would do from the year 2000 all the way up to 2009 2019 but i'd like to stop and i'd like to say that we can go up until 2007 so that can be from zero to seven here which means that if i say 2009 it's not going to work and 2010 is not going to work and 2020 is not going to work and 2300 is also not going to work so that's how i would do the years so let's grab that and put into our text file brilliant so now that we've got everything simply done we now need to do some formulas and base things on how this makes sense so you cannot have 29 days in february if there's no leap year you can't have 31 days in april because there's only 30 days okay so let's clean this up and let's start putting this whole thing together so let's start playing with this now what we're going to do is we're going to break this up into different stages so based on our debug x here we've got a couple things firstly we're going to start with this 31. so any month that is a 31 in we're then going to check what separates they've got we're then going to apply it to those months we're then going to look at this separator and say look from reference one and go and look and make sure that's the same as that one and then from there we're going to add a year that's the basic one we're going to start with so we're going to say the first one is that we're going to start with a 31. we're then going to open up brackets and we're going to put our separators inside there copy that paste that there and close those brackets so what you'll see so far is it's matched all of these so now what we need to do is we need to say what months are we looking at in terms of ones that have 31 days in so the ones that have 31 days are january they are march and all the other ones so let's go back to our original thinking here so there's what we're looking at so 31 are those particular maths so we are now going to do a bracket and we are going to say that zero doesn't need to be there so that is optional um and then we have got all of these so remember how we spoke about these particular months so i'm going to copy that i'm going to dump that in there so the particular months we're talking about are these so it's one march is the third may is the fifth uh july is the seventh august is the eighth and what we are going to do is just close that right now and this will be represented as o1 or o3 or o5 or 07. and because this 0 is optional it is also represented as 1 3 5 and 7. all the way up until 8th or august now what you'll notice is that october and december october is the 10th month and december is the 12th month so if we start putting that in as 10 or 12 those are not going to match so we need to do an alternate here so let's do an alternate and we're going to say it starts with a 1 this time and the options are either a 0 so it'll make a 10 or a 2 and that closes that and if i close this bracket you will notice that we have got a 31 then it's matched 01 and we obviously haven't done the rest of that yet so all of the ones that start with 31 and any kind of month except for these ones over here so it doesn't have january yet will match so now that we've got that we're well on our way to getting this put together the second part of this is that we need to match january and march and may and july as well in this particular pot so the first one is zero and up until there then we're going to alternate and then with one and zero and two so now we're going to have another alternate and inside this we're going to have a bracket and i'm going to close the bracket now so that you don't lose track of your brackets and inside here we are going to have various options so we can have jan alternate march alternate may and december so all of those basically tell us we can have 31st of 01 which is january 31st jan jan jan and if we change one of these jans to a march for instance it will still match because we have a date that starts for 31 if we put in april in here for instance or april it's not going to pull it out from here because we haven't said that april has 31 days now just to go back to our visualizer again so that you have an idea we have gone this route over here and we are saying we're looking at months with 31 days we've checked the the separators then we've said either go from 0 1 so that is january 0 3 all the way up until august or go this way and start with a 1 and then go 0 so that is october and then 0 then 1 and 2 which is obviously december we've then gone the third route over here and said pick one of these months from this list and now we are at this point over here and we can say look at reference one which is group one and we have said basically if you've got a forward slash over there you should theoretically have a forward slash there too it just makes sense okay but we'll get to those just now what i want to do is i want to finish the second part that comes down here so this is we're looking at two things we're looking at months that have 29 days and months that have 30 days so every month except for february in a leap year we'll have 29 and 30 days so you can see that there so let's now add this one to this and i'm going to wrap this entire thing in a bracket so i'm going to put a bracket from there through to there and i think what i'm actually going to do is i'm going to name this so i'm going to name this less than sign greater than sign okay and i'm going to say months with 31 and that's just so you can you can now see what the bracket is and it's obviously this light green and finishes the light green over there so this is called months with 31. now i'm going to do an alternate and i'm going to do the next one and i'm going to say this is months with 29 and 30. okay so let's say we've got months to 29 days or 30 days but let's put a bracket around this and let's just name this and say months with 29 30 and that gives us this so that is a another one there and close that bracket and let's close that complete bracket over there so now we're saying if the month has got 29 or 30 days what are we going to do well we firstly need to get another i'm going to grab that one from the first one and what you'll notice what you'll see is that it ends with a bracket so now this bracket over here ends over there so i'm going to put it inside here and put it there so now let's see if this works so what you've noticed over here is we've got 29 02 29 of february uh 30th of february so so far it's matching but now we need to say what month it needs to match so over here we said that the month was any of these so if i grab this whole thing again and let's copy that and i'm gonna put that inside this bracket again and this time it's saying months that have 29 or 30. so we've then got the split the separator it then starts with zero and goes one it misses out february because remember these are the ones that go 29 and 30 so january march all the way through to december so we can then say zero one leave out two and then we can say three four five six seven eight nine and then under this will be 10 11 and 12. okay so now obviously you can make this simpler you could say instead of 1 3 like this you could say one comma it's gone on to the next line and just come back to that line one comma three to nine and then over here we can say 0 to 2 like that and what you'll notice is that these don't match anymore because none of those will be in there because remember 0 and 1 comes and 0 and three zero and two doesn't and then over here we've got january february march let's now do another alternate april sorry that was stupid april may alternate june july august september october november alternate december okay honestly i just deleted that bracket by mistake okay so now we will see that we have got the 31st are matching from this first part and we don't have anything with this so let's add another one here let's say the 30th of march 2000 and let's say the 29th of april 2000. so that all matches now okay so what you see now is that we've got months for 31 months to 29 or 30 which is now done these two parts over here we've got this one over here which is february 29th during a leap year and this very complicated thing over here is how to calculate a leap year but let's go to the fourth one over here and this is any months that are from 01 or 1 the first of the month and they go all the way up to 28. so that should be every month okay so this fourth option is now going to be another alternate so we're going to say alternate and we're going to open another bracket and let's call this the same thing let's call it all months with 28 okay and let's just close that bracket and then inside here we now need to say what's going to happen so we need to have all the numbers all the dates that start with zero one or one all the way up to the teen dates so that's 11 12 13 all the way up to the 20s all the way up to 28 because remember 29 and 30 are covered by this one and 31 is covered by that one first one is going to be the number zero and that is optional it's going from one to nine so that is if you put a zero to nine there you can't have the zero of may it just doesn't make sense so just be careful of that so that's one to nine then the other alternate the ten dates so that is starts with a one and that goes from zero so that's ten all the way to nineteen and the third alternate is that starts with a two and it goes from zero so that's twenty all the way to eight which is twenty 28. perfect so now that has got all of our starting dates okay so that's all the first parts of the date we now need to have our separator so i'm going to grab the separator from here that's the group over there copy that and dump that inside the bracket i didn't select the whole thing sorry let's just close that bracket there okay so now what we need to do is we now need to say okay so here are the the days first then we got a separator now we need to have the months and we can obviously represent the months as numbers or as three letter things so i'm going to grab from here which has got most of them i'll just copy that and dump that inside here so we have got the days first then our separator over here then we've got this so that goes from zero and now we can actually go from one all the way through to nine because you don't have to worry about february anymore 10 11 and 12 so that's october november and december so that is perfect and then over here all we have to do is we have to say we have to add february in there so it's january february march april may june july august september october november december perfect so this over here is the second capturing group this is the seventh and this is the 11th so if i want to say at the end of months with 31 i want to reference the second capturing group so if i go all the way to the end over here and say ramas and say two then it's looking at this capturing group which is the first one and then it's looking at this captured group which is the second one and saying whatever's inside here make sure it's the same as that and this is ones with 31. so let's look over here here's the one with 31. here's the forward slash and there's a forward slash if i change this to a dash like that you notice that the whole thing isn't matched now if i take this and put it back into a forward slash the whole thing is matched so that is the first one then in the months with 29 and 30 this over here is the seventh capturing group if i go all the way to the end and say i want to look for the seventh this is under all ones with 29 and 30. so there's 29 there's 30. so if i change this right now it's completely captured if i change that to a dash the whole thing breaks if i change that to a period the whole thing breaks if i change it forward slash everything works perfectly and then the last one over here here's the 11th capturing group if i go all the way to the end and say 4 backslash 11 so all of these basically can happen in any particular year so any year and we need to decide what year we're going to start from and what year we're going to end now in the original thing we decided that we're going to start from the year 1900 and we're going to go all the way up to the year 2007. so let's figure this out and we're going to do it in each of these one at a time so after we've got the forward slash there we're now going to do a year and the year is going to be let's put in brackets like that and close the brackets for the year and this year is going to be anything from 1900 to 2007. let's label this for myself just so it's easy to see and we're going to start with a 19 like that and then you can have any number from zero to nine and then any number from zero to nine so that will give you nineteen 1900 1901 1999 and all of those are covered or so an alternate there starts with a 200 and then we can have any number that is from 0 to 7. so let's now see that how this works we've got months for 31 so here it is then we're looking at the year 2000. so if i say i've got the year 2020 here you notice the whole thing doesn't match if i've got the earth 1600 it doesn't match if i've got anything like that it won't match okay so that finishes the year and what we can do is we can grab this whole year and that's why i normally label it just so it's easy for me to see and i'm going to dump it in the end of this one with the months of 29.30 and i call this year 2 and then i'm gonna put it over here and dump it in there and i'm going to call this year 3. so now what you'll see is that when we've like played around and entered all these years it's now messed around with all of our matchings so right now this is matching two so this is still a second capturing group but this is matching seven and this capture group over here is the eighth capture group this is the eighth simon saying change it's eight and over here the 11th capturing group this is actually the 13th now so let's change it to 13. now if you haven't been following from the beginning you'll probably realize that this regex is a lot different from the one that we actually started with that looks like this one over here and we possibly might get to something that looks like that but each time i do a regex it looks a bit different based on the mood i'm in or the way i've explained it to people and right now this one is making a lot more sense to me than going through all of this so in terms of this let's now try and figure it out so we've now got months of 31 days month to 29 30 and all months of 28. we've added years to all of them so let's just remove that now we've got through all of these and we have said the reference needs to go there and we've got the years so the last one we've got here the 29th of february so let's try and figure this out so if i've got the year 2000 that is a leap year if i've got 2004 that's leap 2008 but then it starts going to 2012 and 2016 and 20 20. okay but if we go backwards now and we say 1996 1992 1988 so what we're going to be doing is we're going to say what's smaller so that you can see i'm going to try and continue this so that you can see so it'll be 1980 84 1980 1976 1972 1968 so if you look at this if the number has an 8 there which is even and this is even then it's got the numbers zero four and eight let's try one more time here so there'll be 1964 19 60 1950 6 19 52 and 1948 so anything that's got an even number an even number there that's a 40 and an even number here has the last numbers of 0 4 and 8. any number that's got a odd number has 2 and 6. so what you'll see over here is i said if it's got 2 4 6 or 8 in that position then the last position is going to have 0 4 8 and if it's got an odd number 1 3 5 7 and 9 then it's going to have a 2 and a 6. so that is how it works so let's try and write this and see how this will work for us just making a new regex here and going to copy all these across just so that we can see if this is going to work so the year can start with either a 20 for all of these or a 19 for all of those so let's start with the 19 first and say this is the first one then the second part is going to be all the even numbers so if it has a 0 a 2 a 4 a 6 or an 8 we go into this first part if it's got odd it's going to be the second part so if it's 2 4 6 or 8 what is it to happen what is it going to be looking for even numbers will give you 0 4 or 8. so 0 4 or 8. so you can see it's matched all of those ones that have got even numbers there the second one and i put this into a bracket and i make an alternation and i'm going to close the bracket just so i don't forget the second one is going to have the odd numbers so 1 3 5 7 and 9. i'm going to close that and what does that have also it has odd numbers has 6 and 2 and that's it so 2 6 close that and that looks perfect so now we have got every single leap year from the 1900s put into that we now need to find all the lipios in the 2000s up until 2007 which is what we discussed so i'm going to do an alternation there and say the other option is that it's going to be 20 and it's going to start for 20. but we only want to go to 2007 so it's actually going to start with 200 and because i don't actually have to think that much on this one i can say well the last number can only be a zero a four and an eight and that is going to be all of it but i don't even want the eight because i want to go up to 2007 so those are the only two so 2008 won't match 2012 16 and 20 all won't match so this is actually what we are looking for in terms of february with leap years okay so now we can do a next option here so we've done months with 31 we've done months of 29 or 30 all months with 28 and i'm going to the last one here so it's going to be an alternation and i'm going to name this alternation let's open a bracket and let's name it let's also close the bracket there and i'm going to say february leap year we're going to match february firstly with a 29 then we're going to have some sort of a separator so copy that put that in there we're then going to have a month and the month for february is either going to be 0 or 0 2 so that 0 is optional put that whole thing in a bracket close that bracket and let's go inside there and say an alternation is the other option is that it's going to have the word february and then the last thing is that it's going to have exactly the same as this so this is a capturing group and this is the 18th capturing group so i'm going to say backslash 18 and then the last one is we're going to dump this whole thing in for the year open a bracket for that put that in and close that so let's see if this will actually work now and let's say the 29th of the second month of 2000 and it doesn't work now what you can see here is that my rejects isn't working perfectly and this will happen from time to time and i'm kind of happy that's actually happened in this one we now need to figure out why it's not working and this is why i label things so well so months of 31 and months for 29 and 30 because then i can break it down and actually figure out what's not working so the way to do this is this i'm going to grab everything after the months of 31 and just cut that out and you will see that my months with 31 is working and all that and the capturing group is number two still and that is perfect and that is all working what i actually think i'm going to do here is i'm going to add a starting there and i'm going to end with a dollar which means that that should now be that thing completely i'm then going to put a i'm going to paste the other things i've just cut back in this is still working but let's see what else isn't so i'm going to grab the rest of this i'm going to cut that and just dump that into my notepad and i'm going to cut the rest of that so all i'll have left here is months to 29 or 30. okay so let's put that second part in there so that we don't lose it so months of 29 and 30. nothing's working in this and the reason why nothing's working right now is because this is saying that this is capturing group 8 and this is actually capturing group 3 right now i'm just going to change that to 3 and see that it does work okay so that is months with 29 or 30 days and that matches both of those let's do 29 slash 10 in here and then the year 2000 perfect so that still matches okay so let's now make sure that it starts at the beginning so let's do a carrot there and a dollar sign at the end of that and just remember before we put everything back this is going to change back to the capture group of eight otherwise the whole thing is not going to work okay so we've now got mansfield 31 which starts the beginning ends at the end monster 29 of 30 starts the beginning and ends at the end here then we've got the let's make sure the alternation is still there then we've got all months with 28 which we're going to play with now and then we've got the february in the leap year so let's just grab all of this cut that and let's remove the february leap years and put that there perfect okay so now we need to figure out all the ones that have got months that have 28 and this is not working completely and if you look at these three down below here they should all work completely so let's try and figure out why this is not working and one of the simplest ways to do this is actually to dump it into the visualizer so if we go into here i'm just going to duplicate this so we don't lose the original one and i'll paste that in and as we looked before doesn't like these labels so let's remove that delete that and this year three doesn't like so let's delete that okay now if we grab these i'm just gonna copy that and put that in here it says it doesn't match those three so the reason why it doesn't match is if you look at this representation it's saying that we've got one that comes in here and then it goes to the group two and the second one comes in here and says are we doing something up here and then it goes to the year we'll actually go straight to the end so the reason for this is because these aren't actually combined in brackets and that's part of that so if we look at this it's going here and this needs to be combined in a bracket so let's push that and then all the way up until the end there and that needs to be combined in a bracket and then we've got the separators now what you'll now notice is that this 13 over here if i can look over here at this capturing group which is with the separators it needs to be the same as that one which is the third one so i'm going to take that and change it to three and now all of those match and obviously we need to make this start at the beginning and end at the end again and now we can dump everything back into there okay and let's put the february in which is this one let's double check all the capturing groups again so this one's number two which is working this one over here is number eight so that's perfect this one over here is 14th so let's just update that one 14th and that one is now working and then the final the february one is the 19th capturing group here and let's just update that the 19th perfect so that actually looks quite clean it's quite easy to read it's quite easy to play around with depending on how you how you want to like use it and the funny thing about regular expressions for me is depending on how you actually use it you actually start building them in different ways and i find that i'm building i build regular expressions when i'm just working by myself in one way and when i'm teaching it in a completely different way so hopefully you actually can see it in both ways and you can actually use it the one where that we started with the beginning and this is the second way at the end so let's just actually just copy this one last time and dump this into okay so that was our original let's dump that into here [Music] and i'm just going to clean up these named ranges okay and now that you look through it you can actually see exactly how it's made and it's very similar to the first one but possibly even easier to read i like the fact that we removed the february 29th from the thing and it has that at the end yeah so i hope that makes sense um obviously depending on what your needs are you might want to use this first example which had no non-capturing groups and and then only a capturing group where we had um the separators so where's the separator there so that would be a capturing group this would be a capturing group here all the other ones are non-capturing groups so that's why they've got the question mark with the codon in there so the last part over here instead of referring to this as the second capturing group and doing that if we name this we can actually do it like this let's say that this is going to be called separator number one so instead of two is going to say a g there and then open and close less than equal in signs and say s1 and this will then say we are looking at the sub pattern name to s1 which is the named capture group over there so now you don't have to worry about playing around with which is the first capture group or the second character group you can actually just name it and that should work like that let's just finish updating all these s2 and this is going to be backslash g and s2 and then finally over here this one is going to be s3 and this over here instead of 14 it's gonna be g and s three brilliant so it still works still works and it's now easier to read and now if you play around with just pulling some of these things out it should work perfectly so i hope that was good if you would like to see more regular expressions examples and learn more about regular expressions please click on the link below for the udemy course and sign up with us please remember to subscribe to this channel and then i'll see you soon
Info
Channel: Taming Tech
Views: 3,825
Rating: undefined out of 5
Keywords: regex, regular expressions, date matching, python, php, powershell, javascript, regex validation, regexp, powershell regex, python regex, php regex
Id: BxwCbPdGhzs
Channel Id: undefined
Length: 57min 48sec (3468 seconds)
Published: Mon Jul 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.