Intro to Regular Expressions - How to use Regex in C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
regular expressions are incredible but they can also be incredibly terrifying to make matters worse you've probably heard that regular expressions are expensive and should be avoided in this video we're going to see how easy regular Expressions can be and we're also going to see how they can be very quick and efficient as well now before we get started there are three things you need to know first if you want to improve your c-sharp skills you should subscribe to this Channel with over 450 c-sharp videos and Counting this is a place to learn C sharp second if you'd like free c-sharp resources go to imcorey.com and click on the resources tab they're going to find my podcast a c-sharp project page and a lot more third if you need a deep education C sharp I have dozens of courses to help you out not only be getting a world-class education but you'll also be helping to fund my free content so that everyone can have a great education not just those who can afford it okay let's jump right into Visual Studio we're going to create a new project that's a console application so I choose console app and let's call this regex demo we'll call it regex demo app and if you want the source code from today's video yes.net6 and no don't don't check this box if you want to source code Fizz this video go in the description of this video and you will see a link to download it you'll get an email asking for email address you'll send you an email with the code in it okay so let's start off from scratch with nothing in here we're going to start from a blank slate and actually what I'm going to do is I'm going to the very top here I'm going to paste in a large code comment and this is a cheat sheet and I want to start you off with cheat sheet because regular Expressions have a lot of uh scary looking things in them but really if you have this cheat sheet you're pretty much set for building your own or for diagnosing someone else's okay so we're going to do is go through and start using this cheat sheet so I'll explain more about each of these pieces in a little bit but let's start right away with a regular expression now in order to have a regular expression we need to have a using statement so let's let's do it right here underneath our cheat sheet using system dot text Dot regular Expressions all right that will allow us to use regular expressions now let's talk about what a regular expression is before you start seeing them a regular expression is a think of it like a a query so if you were in um SQL and you said hey I want to look at this table but not every record I want to say where the last name equals Corey okay that would be a query where you kind of filter down the results well array of Expressions kind of like that in the fact that we say hey I want you to find something but the cool thing is is it's not just as simple as I want you to find the last name equals Corey it's I want you to search this entire text file and find me every phone number in it what's the phone number don't know it could be different and we're going to do that by the end of this video so that kind of thing is a lot more complicated but you can't say hey does this let a text equal this phone number well if you don't know the phone number you're looking for how would you know and that's where regular Expressions come in regular expressions are pattern matching they say okay you've defined a pattern now let me go find the match for that pattern so let's see us and we're going to start by starting small and simple and then kind of grow until you get to that more complex find every phone number in a 5 000 line text file okay so let's start with with something simple we're just going to say um let's start with a pattern and then we're gonna have a um to search string okay so the pattern is here's the pattern to look for and then here's a string to search in all right so let's let's do this let's create a pattern that says hey I'm going to look for let's look so for Tim okay so we're gonna look for Tim that's a pattern now that's just three characters but that is technically or can be a regular expression it's a pattern to look for it's a very specific pattern but it's a pattern to look for so if I were to say um uh Tim Corey okay it's going to find in fact let's do this let's wait on the the search to search string for a minute and we're going to search a number of different ones all at once because it's going to be faster so let's do console right line and we're going to say right in here reg x dot is match or look for a match it's going to ask for two things an input and a pattern so the input is is this Tim Curry and the pattern is our pattern we're looking for so we're going to do is say hey um we actually are missing a paren at the end we're going to say in this expression we're saying hey is this pattern which is Tim is that found in this string yes or no true or false and we're gonna get back a true or false and we're saying console right line here so let's say um really simple all right so we're getting super simple here we're saying hey this console right is going to say Tim Curry true I mean it's found Tim in Tim Curry hopefully but let's copy this and do a couple more times and throw it some curveballs Timothy Corey that's actually my name Timothy now that's cool but what about this you see the Tim there sometimes okay so we're gonna find out if it's in sometimes let's run this we've got is Tim Curry true Timothy Corey true sometimes false okay so I didn't find the Tim in sometimes well why is that well he's a capital T in fact if I changed this let's actually add another one here um let's add another one like this the bottom and we're going to say lowercase t if I did that we get lowercase Tim Corey false even though it's separated out it it should be found right no because it's case sensitive now you can make a regular expression not case sensitive but that's not usually ideal so instead one of the things we can do as we can say this now if you up here to our chart we see that this is a range all right so I have now said either capital T or lowercase t all right and if we run this again we'll see that all four of them are true including sometimes we have found team in the middle so we have found matches in all four of these strings now we wouldn't want to do that with every letter I mean technically it could but that'd be kind of a pain so we could say right here um our options is ignore case all right we can do like I said it's usually not the the best but we could ignore a case here in that case we have the Tim like this and you find all four are true even though this is a lowercase T and so is this okay so you can ignore case again not my preferred but you can do that all right next up let's make this a little harder so what if we said that we're looking for the name Tim only Tim as an eighth meaning capital T lowercase I am with with spaces on both sides so we're really looking for something like this but that's not going to work necessarily so let's just ask for Tim to start and let's run this and see if any of them apply and we know the bottom two won't we have two true and true and false and false right now with just Tim right because these two fail because lowercase T okay but what if we wanted to say that we wanted this let's start let's start with simple let's start with it has to start with Tim well come back up here starts with so we say starts with Tim let me run this and we get true true false false still let's change this up a little bit and say I am Tim Corey true true false false because even though capital T lowercase i m is in the list it's not starting with Tim now remember I said before what if it's looking for the name temp which we could say well I want this to be Tim surrounded by spaces like this one is to see that it's a word so I could do this or I could say slash S if we look up here oops wrong slash slash S we look right here white space so I could say slash S again at the end for white space at the end but notice you get the right squigglies unrecognized escape sequence that's because for regular Expressions we have to use the string literal character the at symbol in front of it because otherwise it says actually you're trying to escape an aspect I don't know what an escape s is it's not faking regular Expressions it's thinking of the C sharp Escape sequences so we want to say this isn't a a string literal meaning take this literally as it is there's not anything special in here now for real Expressions these are Escape characters but they're a skate character for regular Expressions not for C sharp so that's why this tells it for C sharp there's no there's no Escape characters here for regular Expressions when it crosses it it's a pattern it'll say oh that's an escape character so yes it's a little bit different okay so now we're saying is there a space on both sides of Tim if we run this again we'll now see false for all four of them well why is that well that's because I've been modifying the wrong thing whoops this is why it's not great to have duplication in place I didn't modify that so let's do this again um because I messed this up and it might have worked it's not going to but it might have worked if I had said starts with notice it still says false okay it doesn't start with it it's in the middle there okay now let's look for the space at the beginning and end and you run this and we get true so I am Tim Corey is true because we've got Tim standing alone so we've we've said yes the name Tim not Timothy not Timmy not an intervariant of Tim and not sometimes with Tim in the middle we're looking for the name Tim and he found it in the last one but that's really not complete because we found the first one too but there's not a space before Tim in fact if we would change this there is a space before Tim or run this now that's true but it's it's not right I mean we shouldn't be doing that so how do you find where Tim is at the beginning so we found team in the Middle with spaces on both sides but what if Tim is at the beginning well we've got the option of this range right here but we also have this alternative okay so X or Y well we could say let's put this in parentheses so s or the start okay so you said the start of string or that's the or right there or a space let's run this again and Tim I try to space out Tim Corey is true so it found it at the very beginning so now we're finding Tim in more places there it's more accurate to what it might be in a text file where we might find that Tim is the first word in a sentence you know Tim went to the mall well then Tim is the beginning of that sentence therefore there wouldn't be a space in front of him unless it wasn't the first sentence in the paragraph So this is important to know how to start thinking through all different places that could break your pattern so now we're looking for at the beginning or with a space in front of it but what if Tim is the last thing in the line so let's we know that this works we know that this works but let's say sometimes too okay um let's say always Tim because that way there's not tame it in the name to be confused bye okay so always Tim where Tim is at the end and if we run this always Tim is false because there's no space after Tim so what do we do well we're going to do that grouping there where we say alternative so or and what's our n character a dollar sign how we know right up here ends with so either there's a space afterwards or it ends with Tim let me run this and we see always Tim is now true now I do want to point out at this point that there are other ways of doing these things this is regular expressions are like programming in general there's more than one way to attack a problem and I would encourage you to try out the different ways to see this any efficiencies in one versus the other but just because you can do it in multiple ways doesn't mean that your way is wrong it just means a difference and like with programming different doesn't mean bad it means it's different okay so with that out of the way because you may have come across this in a different way or had a different thought on how to do this and that's fine let's talk about this pattern for a minute do you notice how complicated it looks it's got lots of special characters okay so at the very beginning we have one two and then one two three and then one two and then one two three we have what 10 special characters with only four we call regular Alpha Characters that's or I'm sorry five Alpha Characters that looks complicated but if we when we started breaking it down and started looking at what it's actually doing it wasn't complicated it was just we had to put in all the different possibilities so if we start with the very very simple stuff just him and we start adding a little bit at a time like oh a space before Tim well a space or the start and at the end I was based after Tim or a space or the end and you start building this up that's when you get the more complicated statements but when you actually take time to figure out what I was doing it's not nearly as complicated as it looks now with that being said you will see some pretty complex regular Expressions I would encourage you to start off simple and practice these things this video is an intro to regular Expressions it's not going to be a comprehensive look at everything you do with radio expressions and training you deeply in all of it but I would encourage you back to here look at these and start practicing one of the things I tell people a lot is that the only way to really understand something is to practice it if you just watch what I do and go yeah that looks great I got it and then try and do it it doesn't work then you get frustrated I would encourage you to try this out and practice it a few times and start simple don't start with the complex start with simple okay now let's talk next about this Okay so we've been using What's called the static version of regular Expressions the static version of regular Expressions is a static method we can call like is match and this passes in the pattern this pattern is not pre-compiled so what does pre-compiled mean well in random expressions C sharp will turn this into a a pattern a high level pattern that it knows about but it's not in the Intermediate Language that C sharp X compile down into and so when we go to run this it has to turn this into Intermediate Language first and then it runs it we can pre-compile this which means that we compile it once and then it's already done um so that it's faster for all of these runs that's the theory okay let's talk about practice first of all if you're using the static version which we're doing right here whereas we just rather instantiate a class then when you put a pattern in it's going to compile that and run it but then when you use it again it's going to remember it's going to Cache that compiled plan and use it over again for up to 15 plants so as long you don't have 15 different patterns that you're using are 16 different patterns that you're using you usually fine to reuse without pre-compiling now the the cost of that compilation happens here the first time you use it when you use a static version you can pre-compile it at the very beginning of your application and then the cost goes there instead but I want to point out some people look at regular expressions and say oh my goodness they're so inefficient you should never use them and I strongly disagree with that in fact I would like you to test it so let's do this let's um let's run this now this is the the pre-compiled version once you run it once but let's let's run this um this expression what you do is mask the return true we're gonna ignore it okay so we're gonna we're gonna copy us or comment this out and I'm going to say four let's do let's do a hundred thousand so I'll Loop through and run this method 100 000 times now is it a lot yes that's a very that's a that's a lot okay so we're going to run this regular expression match a hundred thousand times now what we're going to do is we're going to create a stopwatch so we're going to say stopwatch and that adds by the way the using for system.diagnostics we'll call it uh stopwatch foreign will say at the very beginning here stopwatch dot start and then after this is done stopwatch dot stop and then we'll print out the time elapsed in milliseconds and then let's make this a string interpolation so put dollar sign in front and we'll say stopwatch Dot elapsed milliseconds okay the total elapsed time in milliseconds okay now again this is not seconds it's milliseconds we can get a thousand milliseconds in order to equal one second so we're going to run as 100 000 times now yes the first time it runs it will be compiled after that it will use that cached version let's run this it's done we've run this in 50 milliseconds 100 000 times 50 milliseconds now let's run again just want to show you something 50. let's run it again 51. let's run again 51. your computer might be different because you might have a different CPU whatever but it's pretty consistent around that but it's so minuscule as far as time goes that a little bit of variation is fine now let's change this to a a not compiled student so we're going to say uh regex uh test equals new regex with a pattern all right this is instantiated version of a real expression now when you use an instantiated version you can pre-compile it but if you don't it will never be compiled or cached I'm sorry it'll never be cashed for the next time it'll always be compiled every time so let's take this out this is about 50 milliseconds right we're going to say test dot is match for I am Tim Corey it's the same thing it's just using the instance which passes in the pattern ahead of time but again this is the not compiled version yet so let's and we're putting this inside the start so even if we compile let's compile first options dot um compiled so as well compile it once and then reuse it over and over again but again we're testing it inside of the stopwatch.start 49. okay 49 milliseconds let's run it again 50. 47. 49. about the same I mean the variance is so little it's not really a variance because the fact that you know anything can affect us now let's not compile it which means it's going to compile itself every single time 41. 42. 43. 41. it is actually a little faster so when people start to freak out and say oh my goodness this is a thing you should never do it's so dangerous it's so expensive listen real expressions can be expensive don't don't hear me say they're not however when we say expensive we don't mean never use them what it means is be smart about it if you have a loop with a million records you're looping through and then you're running three or four different regular Expressions on them yes that's going to be slow but it might be necessary the other part is can you do this without a regular expression so in these I could have said contains you know so does this contain Tim I could have done that now this not so much this is harder to do I could have said starts with Tim or ends with Tim or contains Tim I could do that but that becomes like three different statements to run which is where regular expression starts to shine so just saying look for Tim we could do this another way but if you're saying Hey I want you to find this pattern of things that's probably a regular expression and it's okay to run it just look for any time you're running up millions of times then you need to Benchmark and figure out if it's a different way but if you're running this 10 100 500 times even you're not going to see a performance hit at all but again you should test in your scenario you should know what um where the performance bottlenecks are in your application but performance bottlenecks are something you should find probably near the end of your project rather than trying to pre-do all of your efficiencies if you say you know what we're not going to use for your Expressions because they're slow that's that's a bad take this is a tool that is available and is the best tool for the job in a lot of circumstances so be careful with some of the um the rhetoric around regular Expressions because quite frankly it's not helpful to the what we're doing and it actually hurts you as a developer and not have this tool in your toolbox so I just want to point that out that they're not nearly as inefficient as you thought they were especially not in newer versions.net now if you go.net framework they might be slower but in in.net uh six what I'm doing right here super efficient and again always test you can use a stopwatch just try it out um there's other ways of testing performance but this is a quick way in a little console application to test just that one line to see how efficient it is okay let's keep going and we're going to do is we're going to take on a more complex scenario now let's do this let's move this up to the very top because I want to have let me close this where I use this quite a bit we're going to do is let's bring my solution Explorer here and I'm going to copy a text file instead I'm just paste it here I cannot well no problem I'm going to open a folder in File Explorer and I'm going to paste into it a text file and it's right here test.txt now let's go to the properties of this and say Yes copy to the most copy of newer copy of newer to the output directory and what is this it's a text file and if we look it's a big text file it's just almost not quite it's almost a thousand lines I'm sorry 5000 lines of text and it Scrolls off to the right I think about 80 characters wide or something like that this is lorem ipsum text so I did I went into I didn't use an online tool I just went to vs code and create an HTML document and said lorem and I think it was oh goodness I think it was 50 000 I asked for so 50 000 words in lorem text and I formatted it and saved it but then if we look in here I'm sure you'll I'm sure you'll see it just by scrolling through right uh in here somewhere are six phone numbers I'm gonna give you a way to find them without a regular expression just so you can know where they are do control F and look for five five that's in every phone number in here so the first one is this one on live 530 there's 440-55-5201 this is a I use U.S format for phone number we're just going to stick to the US format okay but there's one with dashes in between let's go to the next one last one at the beginning of the string and it uses dots instead of dashes this one it's a phone number but there's no separator at all this one has the parentheses around the area code a space and then a dash here and then this one kind of has mixed it has a DOT and a Dash and we have dots down here I think that's all of them yep back to the beginning so that's our options for our phone numbers those phone numbers are hard to spot and kind of bury in here and the way we really find them in this mess is if we one looked for 555 which I include in there on purpose or we look for any kind of digits but what if there's other kinds of things in here that have digits well that's going to create a mess because there's a phone number in here possibly but um is it a phone number or is it an ID or is it a zip code or is it and so we want to do is take this text file and use this to really show off how powerful the rare Expressions can be so we're going to do is we're going to look for those phone numbers in that text file let's start by looking up the file so we're going to say string to search equals file.read all text from test.txt now something to point out here first of all I'm reusing this variable this is declared down here so you cannot just uncomment everything and have everything run you need to comment this out if you want to uncomment this so I want to name the same because I want to be as clear as possible but then also we're reading all text now there's different ways to read a text file in you can read all text you can read all lines and so on we're going to read all text which means that all 5 000 lines are almost 5 000 lines of lorem ipsum text is they put into this variable as a string the whole thing now if you had a million rows that might not be efficient you might have to stream the text file and then run it the real expression multiple types which you could do but in our case this works just fine and it's efficient so we're going to grab this but I want to make sure that we have everything so let's do this um let's just say well let's let's create our pattern string pattern equals but then just put a break point here okay and we're going to run this just to make sure that two search fills up that's all we're gonna do break point is hit and two search if you notice has a lot of stuff in it okay so if we Mouse over it and look it's got a lot now that means that we in fact did read from our text file which the text file is located without a path what does that mean is finding it in the same location as our executable that just means that when you go and download the source code again you can get the source code by using the link in the description um but if you get a source code then it'll work for you too because I have it on my machine in a different location than yours but they put it into the project and said copy if newer it's going to put in the same folder as the compiled executable for our application so now we have the entire file in this variable now we have a pattern to create so that's next and then we're going to do is we're going to try to get all the matches we're going to wait on the pattern for a minute and we're going to say match collection or call it matches equals and it's actually already tell me what I should probably do which is regex dot matches again this is the static version and we're saying to search that's the the place to search so that's where we're looking for the matches and the pattern to find is this string right here and so this matches notice it's not the same as we did down here which is is match is matches said yes I found a match but matches says hey find me all not just one but all of the times that this rate of expression matches and what the match was I mean again we're not going to look for a specific phone number and say is it in that file instead we're going to look for a phone number pattern and say what phone numbers do you find there's a difference so with our matches we have what's a search and the pattern it's going to return a match collection which is a set of matches and then we can say for each match in matches and we'll say console right line and we can just say match which is a we can say match.value like so and that's just the actual printed out hey we found this match um so with our text file we could find something like let's just look for Dolores right if we do this pattern match and we run this we'll see it found Dolores a lot that's because it uses um I used that word over and over again when we're doing our lorem of some text but we're not looking for Dolores we're looking for a phone number well let's define a phone number what is it that makes up a phone number well it's going to have digits now notice here we have slash D lowercase which is a digit character so we're going to want to find a digit character now if we just said oops let me say that this is a string literal so if we said this is we just want to find a digit character it's going to find a lot of them in fact we run this there's all the digits found but that's not really a phone number that's just finding every place we find a number we want a pattern of numbers so we want three digits together is the first section and we have the ability to use the curly braces to find a repetition now this pattern right here says repeat low to high so one to three of whatever precedes it so if I were to say you know one comma 3 this would say one to three digits but we don't want that we want three digits now if we don't have the N the Y that says at least one but if we drop the comma y see the difference there if we drop the comma y that means that many so I'm going to say three digits I'm looking for specifically three digit patterns now if we run this we'll see we have a number of three digit patterns but that's not good enough because that's not the com that's not a complete phone number that's just the first part but then you want to say okay I want to have a another three digit pattern okay another three digit pattern and then a four digit pattern so what this would what this would find let's just put a search here this would find the 440-55-1212 or whatever the actual phone number is that's what this would find now I could have just said digits of 10 and find this but let's run this and sure enough it found just one 440-55 uh four five six seven that's the one phone number that matched this pattern exactly but we have options for phone numbers for example sometimes people oops I have 124s there sometimes people put the dashes in between well let's handle that so in between our first three and our next three we need to have a dash so we're just going to say Dash and we'll do Dash between the next three and this is a regular character so it works just fine just as a real character if we run this we found the one has dashes but we didn't find the one we just found because we don't always have dashes and that's where this character right here comes in the optional preceding character match which means that if we put a question mark after our Dash we're saying it can have a dash but it doesn't have to have a dash okay so let's run this again and now we see two matching numbers we found the one that has no dashes as well as the one that does have dashes now let's look at what else we could do well sometimes people will put the dot in place okay so let's take care of the dot as well we already have a Dash net please how we do a DOT well we can do the or so it's either a dash or a DOT so if either of those is true let's do this for over here as well if it's either a dash or a DOT and it's optional is this optional now applies to everything inside the parentheses so it can have nothing it can have dashes it can have dots let's run this again and now we have five phone number if you have the dashes we have the dots we have nothing we have one has mixed dots and dashes and we have the other one that has dots so we've now found five different possibilities we haven't found the sixth so where do you find the sixth possibility well we can look for the one that has this this is another way that yes we have a lot of different ways to create phone numbers right this pattern where you have an open paren and a closed paren and then you have a space or not but usually a space and then you have a dash well in order to do that we just start off by looking for an optional parentheses so we can say uh parentheses and a question mark me optional right well not quite remember we use parentheses to say to use this for an alternate alternate so how do you put an actual parenthesis in here we have to escape it so now you've escaped that to say no it's just a parenthesis okay so we're looking for this as a optional parenthesis and then we'll have to look for an optional parentheses at the end all right so we're going to say slash parentheses to escape that parentheses but that's optional and then we could have a dash or a DOT we also have a space so let's have another option here slash S for space whoops wrong slash slash S for space again that's up here white space so now we have a optional opening parenthesis we have our three digits we have an optional clothing parenthesis closing parentheses and we have an optional space dot or Dash now why didn't I just put this inside of my my set of ores well because it's actually two characters that may or may not be there so this is optional but if we have this parentheses we probably have a space as well so that's where that ore comes in we don't have to so that's why I've separated out and yes now we're starting to look like a very complicated statement we've got all these slashes and Squiggy brackets and question marks and dashes and dots and all the rest we're saying man that looks complicated but we broke it down figured out what it does it works great we've now found the six phone numbers that I buried in that text file almost instantaneously this thing is super fast so that's why regular expressions are so powerful if you decide you want to look up and find a phone number in this whole huge text file you think you know I think they left me a phone number somewhere there you go quick look up if you want to pull out certain patterns like an email address or email addresses are hard by the way don't think that you've got to conquer there's some complexity around an email address but phone number zip codes addresses names words so many different things you can find using regular expressions and while the statements do start to become rather complicated at least looking when you start to break down exactly what they're doing in reading them the character by character or two characters at a time sometimes like for example okay it's an Escape character what's it escaping a parenthesis so it's just a a character the parentheses character this means the proceeding is optional so that is optional okay this means a digit this means times three so I'm looking for three digits with a possible parentheses in front of it and then a possible presence afterwards you start to read through this and go wait that's not that complicated it's just it looks ugly it's not easily human readable you don't just look at this at a glance at least I don't and go oh yeah look at our phone numbers that that's hard to do but when you start to slowly Build It Up over time you create this complicated statement that does exactly what you're looking for now we could make this even more complicated and we probably if I was going to create this for a customer I probably would make it more complicated and the reason why and this is where you start to Think Through uh and use the logic you've been building as a c-sharp developer think through the edge cases now it's found all the ones I put in there however what it only has phone numbers in that file that are actually numeric the rest aren't phone numbers so I would throw in there a few other things that aren't phone numbers maybe a um maybe an ID code for example if we had you know uh one one two dash three four five six seven eight nine um zero that's technically 10 numbers make sure that doesn't trigger a phone number that's an ID number that's not a phone number and then you have to think through or if we put the one plus if you're in the US that's the country code so you had you know uh or plus one like that that we're not picking up the country code so if it's a different country code that'd be a problem it doesn't work with international phone numbers we'd think about that um do we think through things like well what if there is a um I don't know you know if I were to say one two three and then we're gonna say something else you know maybe a zip code and I say like that is I could get triggered somehow by a phone number lookup and start throwing these edge cases out to see if you can figure out well what if there's a you know is that a phone number no it's not a phone number because it's not the right number of characters but it Woody picked up the phone number because it found this and so this is a phone number so I need to think through okay kind of like our name here above or down below actually now um does it have to have a space around it does it have a space on both sides if it does then you have to look for the spaces around it as well and then think through but what if it's the beginning of the file or the end of the file and you can start to get more detail that way as well and your rare expression will become more and more complicated as you do but you'll be dialing in more and more specifically what you're looking for and what you're not okay so these are super useful in certain circumstances now that you know how to use a regular expression don't look at every lookup problem or query problem as a option to use regular Expressions okay make sure that the the solution fits the problem this is a tool is not meant to replace every other tool you have use it when it's wise to do so when this is the the best option if there's another option that is simpler use the simpler option because when you're scanning through your code that's kind of a road or a speed bump okay remember I I I've said a number of times that source code is meant to be read by humans not computers humans that's what's meanty read by now computers do read it and they do compile it but remember computers compile things down to ones and zeros we don't read ones and zeros we read C sharp code and that's why it's mentally read by humans because we're gonna be the ones debugging it we're the ones that try to understand it and to improve it and so when you read down through source code it's very easy to read write console.run rightline okay that's writing to the console cool stopwatch okay I understand what a stopwatch is stopwatch dot start I got that stopwatch dot stop I got that even if you didn't know about a stopwatch class before this video you probably have a pretty good grasp on what it's doing it's starting and stopping a stopwatch that's human readable code when you get to a regular expression and you see pattern and you see that you go I have no clue and so you have to stop your reading you're perusing and say okay what is this doing and you start one character at a time and if you're like me maybe you're not maybe maybe you look at Radio expressions and go man I got it that's that's super simple that's a that's a you know that's a phone number no problem I see the 334 pattern in the question mark got it but for me I have to go through and read character by character and write things out and and kind of figure things out it takes me a long time sometimes to figure out what a right expression is doing or trying to do one thing would help and I highly encourage this people say some people say that you should never have code comments that's wrong that's just wrong whenever you say never in programming you've probably made a mistake because if you go to an extreme always or never then you are leaving out any possibility for an edge case and life is full of edge cases if you speak English you know there are tons of edge cases in our language itself so edge cases are a thing because the world is unique it's not like we fit in the same pattern all the time otherwise we wouldn't need software developers we would have solved every single problem because it would have all been known but no we have edge cases so when it comes to code comments I don't recommend commenting things like the stopwatch dot start that's pretty obvious what it's doing that's code that you can easily understand but this I would say this pattern matches phone numbers I might even say in the following patterns and specify them okay um the only thing is and this is where the no code comments people have a good point is that if you come back and say oh we need to adjust this to um also take into account the idea of people using I don't know um commas instead of periods as well okay well you need to update your code comment as well because otherwise your code comment is out of date and it's actually worse than not having a code comment sometimes because the code comment is what's easily readable and some people read that and believe it and if it doesn't match what's actually happening that's a problem so if you do have a comment you have to maintain it otherwise it's actually worse than not having one but in cases like this it's important to have a comment to make it easier for people that are scanning your code that don't necessarily need to handle update a pattern because maybe they're just looking for another part in this this section they don't need to understand everything that that is doing they just need to know oh that matches phone numbers and that's we all we need to know and so that code comment is very very helpful for that so that is the intro to regular expressions I could go on for a full course on just regular Expressions there's lots to get into it with them there's lots of ways to make them more efficient and to choose different options for the same pattern and there's lots of cool patterns you can create but I think this gets you started and again up here we have all of well not all but a lot of the most common things you can do the expression and hopefully that will help you get started looking for things I encourage you practice what you've learned today don't just watch this video and go okay I got it let's move on I'll use it in production when I need it practice it create a console application nothing complicated it's a console application and do some testing and then you know what if you have questions later on throw in your your stopwatch and say hey is this efficient is it not efficient um is there a different way of doing this it can I compare to if you or you might get into you can use the Benchmark benchmark.net which I've got a video on where are you you know get more in depth into benchmarking and do different options but really like I've shown you it is super efficient in most cases to use a regular expression as long as you're not using it millions of times so with that that gets you started let me know down in the comments what your thoughts are in real Expressions how often you use them and you know if you feel more confident now in using them thanks for watching and as always I am Tim Corey [Music] [Applause] [Music] [Applause] [Music]
Info
Channel: IAmTimCorey
Views: 34,262
Rating: undefined out of 5
Keywords: .net, C#, Visual Studio, code, programming, tutorial, training, how to, tim corey, C# course, C# training, C# tutorial, .net core, vs2022, .net 6, regex
Id: R5BcHIMZMxc
Channel Id: undefined
Length: 55min 52sec (3352 seconds)
Published: Mon Oct 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.