Beginner's Guide to Regular Expressions in Rust πŸ¦€ Rust Programming Tutorial for Developers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys my name is Trevor Sullivan and welcome back to my video channel thank you so much for joining me for another video in our rust programming tutorial Series in the last video in the rust programming playlist that I've put together on my YouTube channel we covered the process of reading CSV or comma separated value files using a special CSV package we're going to kind of build on top of that theme of parsing data by explor in the ability to use regular Expressions inside of rust programs now if the concept of regular Expressions is something that's new to you then you'll want to probably go through some pure regular expression tutorial Series so that you better understand the syntax of regular Expressions we're going to take a look at some simple examples of regular Expressions but to truly Deep dive into the different syntaxes that you can use to express different patterns within a regular expression string then you'll probably want to just dedicate some time to learning about the reg X syntax it's a really cool language it's extremely powerful and once you know how to parse using regular Expressions it'll open up a lot more opportunities for you to build helpful Utilities in the rust language and many other languages that support regular Expressions as well so that regex knowledge can kind of translate to many other languages it's not just rust specific so we're going to be taking a look at this crate here called regex now rust the standard Library does not include support for regular Expressions if you want to do regx parsing you'd either have to write it yourself which is presumably a lot of work or you could just use this off-the-shelf regx package here which is available on the crates.io registry now this is a pretty straightforward package to use very similar to the CSV package that we looked at in the previous video it is a pretty straightforward set of data types here that we can work with specifically there is a regex struct here that's exported from the reg X crate and this is what we're going to use to create a pattern and then perform captures against an input string now the terminology that's used in the regx crate here for the input string is actually something called the Hast stack so the Hast stack is basically the text that you want to perform a match against using a pattern that you've built your regex object with so it's pretty straightforward all we need to do all is call this new function here when we call the new function on the regex type it basically just takes in a static string value here and that's going to be the regex pattern that you want to use to detect certain patterns within a given set of input text once you've constructed that regex you can go ahead and reuse that pattern you can can even declare the pattern as a separate string so if you want to construct multiple regular expression objects using different patterns or maybe join those patterns together whatever it is you want to do uh you can basically just declare that as a separate string value and then pass in the a pattern as a variable rather than hardcoding it into the new function call here now when you call the new function it's going to return a result where you might potentially get an error if maybe you specified a bad pattern or something like that so you will want to make sure that the result is okay so there's an isore okay function that we can use on the result type to make sure that we don't actually have an error and that we do have a regex returned back to us so once you unwrap that resulting regx object there then we can use some helpful functions or methods right over here in order to detect patterns one of the most simple examples is is to just detect if there is or isn't a match so this would be something like if you wanted to validate an email address you could say just tell me true or false does this input text match an email address and if it doesn't then you can you know reject that email address input and ask the user to re-input a proper email address or if is match returns true as a Boolean value then you can go ahead and take that input and do something f with it like write it into a database somewhere for a mailing list application so that's a really simple example where you just basically are detecting if there is or isn't a match however if you want to extract data out of a string let's say that maybe you have something like a CSV file format right and you need to come up with some kind of custom parser or maybe you're parsing log files and you're looking for year month day and then hour minute second and then maybe a warning level like info warning error something like that and then some random message following that error level well in that type of case you're going to need to actually extract you know what is the error level is it info is it warning is it error is it debug you know what exactly is the message level what is the actual message that's being passed into the log and then what are those individual field values for the year month day and then hour minute second that's where we want to actually perform data extraction and go line by line through a log file and pick out all of those individual Fields using a regular expression so those are just a couple of examples where you might want to use regex but in the case of a simple email match you might just detect if it's true or false as a match but in the case of you know parsing a log file where we actually want to extract the values of those individual fields we can actually use this function here called captures and this will return just the first match if you have an array of matches so if you have let's say a multi-line log file where you have one log entry on each line of the log file then you can also use this function right here called captures uncore iter and this will return an iterator over an array of matches that you can use to detect a series of multiple matches within a given log now if you do need to do something like par ing a log file or parsing a CSV file that typically uses a new line character as the row separator character then you're also going to need to set up multi-line mode in your capture so to do that there is a another type here called regex Builder so if you watch my CSV video that came just before this that I recorded yesterday then you know that we had the CSV reader and the CSV Builder that allows us to kind of cust customize the reading abilities or options that we have on the actual CSV reader well we have a very similar pattern here where we have just kind of a core regex type with some kind of samean default options but then there's a separate object here called the regx Builder and if we create a new instance with this we once again pass in the pattern here but then before we actually build the regx object itself that we just looked at before we can actually specify some custom options here there's a multi-line method here that allows us to set the multi-line option to enabled right here you can also specify inline options with regular expression patterns too so there's kind of a separate syntax that you can use directly inside of the regex pattern at the very beginning that lets you enable multi-line mode but if you prefer to do it from your rust code you can just call this function on the Builder and then that'll return the updated Builder back to you and then finally once you've specified any configuration option options that you want here you can go ahead and just call the build function right here and once again that'll return a result that's wrapping your final regex object and then you can use that regx object to call things like captures or captures iter or find or is match and so on and so forth now another more advanced use case for regular Expressions is to do a search and replace so if you want to search for a certain pattern like any word that starts with T I want to replace the first character t with an S instead so if I wanted to do that I could write a regular expression pattern and then I could use a function like the replace here in order to specify a replacer that takes a certain input and then replaces it with a different value so that's another nice option about using regx is that you can look for very Advanced patterns and then you can perform Replacements on those patterns and this helps you to manipulate very large data files efficiently so what we're going to do is jump into some code samples where we install this crate and then we're going to take a look at how to create one of these regex objects and call some of these helper functions in order to detect patterns inside of input strings now before we get to the actual code sample I wanted to invite you to subscribe to my YouTube channel since I'm an independent content creator any kind of support that you can lend to this channel is extremely helpful so if you subscribe if you like like the video that you're watching right now if you go through the playlist and watch the other rust tutorial videos that we have in this rust programming playist that would be great as well there's probably a lot of different Topics in here that will help you especially if you're new to programming in general or maybe just new to the rust language specifically we cover a lot of advanced topics in here like parsing Json data parsing CSV data threading inside of rust and a whole bunch more also I have a link in the pinned comment down below to my Amazon storefront and anything that you purchase directly through that storefront will go to helping support this channel as well so that I can bring you more videos like this one all right so what we're going to do is jump over into our editor here so I'm going to be using a vs code connected to my remote Linux virtual machine on one of my lxd servers that's running in a different room and what we're going to do is go ahead and just spin up a new project here so I'm going to do a maker and then we'll just call it reg x- test for now and then I'll do contrl krol o and just open up that directory in my editor here now once we've got this project folder open we need to just do a typical cargo init that's how we create a new project and kind of scaffold things out so that we can spin up our application here and then as soon as we do that we'll have a series of files in here one of those being cargo. toml so if we just C at cargo. toml you'll see by default we don't have any additional thirdparty dependencies installed but if we head over to crates.io this is the package registry where community members can upload their dependencies or libraries that other people can consume in their applications if we just do a search for regex you'll see that we've got this regex crate right here uh it's very popular it's got over 172 million installs so that's pretty popular and it's got whole bunch of version history and if you take a look at the dependence tab right here this will actually show you a list of some of the most popular packages that actually consume the regex package so you might actually kind of look through this list and see if there's any additional crates that you might be able to use that also work with regex in any case let's go ahead and add this to our project so we'll just do a cargo ad regex here and there are some optional features available in this crate so as you may know crate support optional features that that you can enable during the installation of that crate but we're not going to be using any of those right now because all the core functionality is available with the default feature set so let's go ahead and just do a cargo add regex down here that'll pull in our default feature set here as you can see in this list there are a few that are disabled by default here but again that doesn't really matter too much because we'll be able to use the core function ity so now if we head over to the main. RS file here that's our entry point into our application and I'll go into zen mode here to provide a little more screen real estate so let's go ahead and just get rid of this hello world line and we'll build out a function called test regex and we'll call that from our main function that'll just help keep our main function clean so that if we want to come up with any more test scenarios we can just kind of segment those off into separate functions and then just call those those functions from our main function all right so inside of this test reg X function here the first thing that we want to do is go into the reg X crate here do double colon and we can instantiate this regx type here now if you don't want to type out the full path to that you can also just go to the top and say use reg X regx you can also put these use statements inside of your function so it' be perfectly valid syntax to put that inside of my function but since there may be other functions that I Define inside of this main. RS source file I want to make sure that I declare it at the root of the crate here so that I can declare those additional functions and still be able to use this imported type so now what we'll do is just do a regx with a capital r that refers to the regx struct that we imported and then we'll just use the new function here to instantiate this type now the re parameter here for the new function is going to take a input of a static string so we'll just say let my pattern equal something as a double qued string here and then we'll go ahead and do something like a to z and let's say that we're just going to search for a first name so like Trevor or Nancy or Joe or sha or something like that right so any of those names would range from maybe three characters to let's say eight characters so what I'm going to do here is specify something known as a regular expression character class this is surrounded in square brackets and I can do a kind of a range of characters here like lowercase a to lowercase z i can also do uppercase a to uppercase Z I can also do ranges like 0o through nine if I'm trying to detect digits for example if I was parsing a file and looking for a year or a month or a day then I might want to specify 0 through 9 and then a quantifier of four and that would help me to find a a year like 1972 or 1993 those are four digigit years that all use digits 0 through 9 right so this is a simple pattern to detect a year uh maybe I could detect a literal Dash as well and sometimes you might have to escape these so you might have to put in a backslash here in order to escape that character because it has special meaning within the context of a regular expression pattern and then after that we could say you know 0 through 9 again and have a quantifier of two so now we're looking for a four-digit year and then a literal Dash and then a 2digit month and then I could just do the same thing again and look for a two-digit day following that so that's an example of how you might detect a date pattern in a log file but in this case we're just going to be searching for a first name so I'm going to do a to z lower case and a toz capital characters here or even to be a little bit more advanced we could could say A to Z Capital and then specify that we want a quantifier of one and then we could say lowercase A to Z and we could have anywhere from two extra lowercase characters all the way up to maybe eight extra characters so basically this is a pattern here that's always going to look for a starting character that's a capital letter and then any number from 2 to eight characters following that that our lowercase and so that's kind of a good indicator of a first last name or or middle name for that matter so now we can pass in my pattern as a variable into reg X here and the reason we're getting this error is because we don't have a semicolon at the end here and our function here doesn't have a return type so it's considered the unit type in Rust and if you don't have a semicolon at the last expression in your function then it's going to try to return that value so it's essentially the same thing as doing a an explicit return here and so we want to make sure that we put a semicolon at the end here and that will suppress rust from returning this expression back to the collar of this test regx function so now you can see that we get this result returned from this new function so we want to capture that so we can actually use the reg X object so I'll say let my reg X or maybe call it name reg X just to be a little bit more explicit there and then this is going to return this result that wraps our reg X and potential an error if there's any errors in our pattern here during instantiation so now that we have this object we need to first check to see if it's okay so we'll say if name regex do is a then we'll go ahead and maybe just do a panic here so we can use the Panic macro to just panic and say you know error in regex pattern but if it's not an error error then we want to do something some kind of detection or data extraction with this regex so let's start by just taking a look at the simple function over here on the regex type so let's go down to stru and then regex and we have that really simple is match function here and that's just going to return a boou on a given input Hy stack string right so the input text that we're detecting is called the Hy stack so anytime that you see Hy stack in reference to the regex crate that's what it's referring to so let's define another variable up here called input text and we'll just set that to something like Trevor for now and then we can just change that later on to some other values so that we can test out some others so then what we're going to do is to use the regex object so we'll say name regex dot unwrap and that'll give us the actual regex object and it un unwraps it from this result object here and then once we have have that we can go ahead and call the is match and then we can pass in the Hy stack value which is going to be our input text variable so let's pass that in and the result from is match is just going to be a Boolean value either it does or it doesn't match right so let's say let match result equal that and that'll be a Boolean type and then we'll just say print line did input match pattern and then we'll just print out the value of match result inside of that print line statement so let's go ahead and give this function a try here so we're basically just declaring this pattern we're instantiating the regs with that pattern that we've declared and then we check to see if there's an error in here and if there is then we'll just Panic our application or you could just do something a little bit more polite like you know print a print an error message with print line and say error in regex pattern and then we can just return the function early so that any of the code down here doesn't actually execute if an error is detected but it's kind of up to you what approach you want to take so we'll go ahead and just leave the panic in place and then we'll do a cargo run so a few dependencies are going to get compiled in here finally our application and as you can see it is returning true because our regx pattern did match our input text so we have a capital single capital letter here followed by 2 to8 kind of a range uh definition here of lowercase characters so what if we changed the input text to be something like TR so we'll keep the same pattern that we've already declared but we're just going to put in a single capital letter and a single lowercase letter do you think we're going to get a true or a false in our match well if you said false you would be correct because the T and the r the T matches just fine it matches this expression right here it's a capital letter a single capital letter to be specific but then following that single capital letter we only have a single lowercase letter and we indicated as a quantifier to our regular expression that we wanted to have a minimum of two lowercase letters directly following the uppercase letter so in this particular case this two character word here is not going to match our input pattern however if I did TR then because the re matches a minimum of two lowercase letters you can see that that will match just fine now we could go back and change it to tr and then if we change the quantifier and say I want a minimum of one lowercase letter then you should be able to see that that does match as well so now we can plug in pretty much any first name that we want to like Daniel for example and that should match yep it did and we could do something like Joe and we get true once again that does match we could do Sally do do that and it matches sure enough we could try Nancy and see if that matches as well so all those different first names are going to match just fine right now what if we want to get the actual name that was passed in right so one of the things we could do is pass an input string that's a little bit more complex like Nancy is going to the store and so now what we're going to do here is detect the very first word but we're going to omit the anything that follows that first word because what's implied here when we do a regex match is that we're starting at the very beginning of the string so you have to think about there kind of being a cursor that has internal state in a reg X and so the regx engine is going to start at the Le hand side of the string and it's going to progress one by one through the characters in the input string and it's going to attempt to match them to the regular expression pattern that you've specified in your input pattern string and so what's going to happen here is it's it's going to look for this first identifier here it's going to say okay do I see a capital letter yes I do and then following that it's going to look at the a here and say does that fall into this pattern here where we have lowercase A to Z and a quantifier of 1 to8 well yes it does does the n match that pattern as well we're still looking for up to eight yes it does does c match that pattern yes it does does y match that pattern yes it does but when we get to this space here the regx engine tries to do a comparison against this pattern here and it says Hey a space doesn't fall into this a toz character class because a toz only contains the individual characters from A to Z it doesn't include any special characters like asterisks or backs slashes or forward slashes or dashes or anything like that and the Whit space characters like a space or a tab character are also going to be excluded from this character range so as soon as the regx engine identifies a mismatched character in this input string it's going to see that that's the end of our pattern so it's not going to try to match any more characters in the remainder of the string so what this allows us to do is to define a input string and it doesn't really matter what name we put for the first word here that's always going to be the name that's returned by the regx engine so now of course we already know that this go this is going to match right so if we do a cargo run it's still going to match because this pattern matches but what if we had different sentences like Nancy is going to the store or Joseph is going to the store right those are completely different names but if we had a text file that had a whole bunch of lines with different first names and all we wanted to do was extract the first names from that file then we would need some way to dynamically detect what that first name is from each line in the input text file right so we want our regular expression pattern to work work regardless of what the input string is as long as it matches this General pattern so what we'll do is go down here and instead of just calling do is match here we actually want to get the results we want to actually capture the value that was being returned back from the pattern match and one of the ways that we can do that is to use this find function here so what this is going to do is it's going to return the first first match that it locates using that regular expression pattern it's not going to find multiple matches it'll only find the first match so what we can do is change this call here to find and once again all we have to do is pass in the Hast stack as our input value but the key difference between is match and find is that the find actually returns a match that's wrapped inside of an option so the option allows us to check to see if there's none so if there was no match then we would just get the option none variant returned or if there is a match then we'll get that match in the option and we can just call the unwrap function on it and that will allow us to retrieve the underlying value so what we'll do is say do find then we'll pass in our input text variable here and that should give us this option match here so the other thing we could do here if we didn't want to check to see if the option has none or sum what we could do is say unwrap or and we could specify an or value and just say invalid so like no value was detected right and because it's returning a match we would actually have to construct a match object instead but I'm not going to do that in this particular case so we're just going to say unwrap here and actually there's a little bit better way to do that so what we'll do is say if let uh match some match equal match result whoops let's close off that parentheses there so then what we can do is use this syntax here in order to set a variable like my match for example you can't use keywords like the match keyword which refers to a looping construct in uh not really a looping construct but a controlflow statement in Rust so now this my match variable if match result does have a value and it's not none then this my match variable will get populated with the actual match object and so inside the context of this if statement or this block we can go ahead and use that my match variable and if we check out the documentation for the match type in the regx crate here let's go down to strs and then click on the match documentation here you'll be able to see that we can get the underlying value that's returned from the match as a string by using using the as string function right here we can also see where it starts and ends so we can see inside of the string which character position it actually starts at so if it starts at at zero at the first character then the start should return zero but if it detects that name somewhere further down in the string then it would return where wherever the starting character appears inside of the string starting at a zero index you can also see a length so basically how long was the name at the moment we have a regex pattern over here that has a variable size the name could be as short as well now three characters because we have a single capital and minimum of two lowercase here but we could have one capital letter followed by eight lowercase letters and that would result in a total length of nine so in that case we could have a range anywhere from three all the way up to nine characters and so we kind of need to know well what is the length of the final match right so the Lend function here on the match type is going to allow us to detect exactly how long that matched name is all right so let's go ahead and give this a shot we'll go down here and say uh not did input match but what is the actual match value so we're going to put in a placeholder here and say who went to the store question mark and then we'll pass in my match dot as string and that will return our match as a string value that we can pass into this string template so let's give this a try we'll do a cargo run down here and as you can see it says who went to the store well the answer is Nancy because our input string right up here says Nancy is going to the store but using variable shadowing we can overwrite that same variable and uncomment this line by saying Joseph is going to the store and so now if we rerun our application the latest value of input text is going to start with Joseph so now you can see that Joseph was parsed from the regular expression or I could change it to something like Trevor and that's also going to extract just fine um if I say Joe Schmo is going to the store then that's not going to match because I put a second capital letter and it wasn't expecting that but if I put a lowercase that'll parse fine as well as long as it's a maximum of nine characters including the first capital letter it should match just fine here so that's how we can extract the match value from our regular expression but things can get a little bit more advanced from there let's say that within a single match that you wanted to extract multiple values so in regex terminology we call these capture groups you can do a single match but within that particular match you can have multiple groupings of data that you want to extract from the regx pattern so let's consider an example here let's do a input string here I'll just get rid of that duplicate line and then I'm going to change this input string to some kind of CSV is pattern right so I'm going to say Trevor and then pipe character and then Sullivan pipe character and we'll say what year is it right now right in Trevor's life what year is it right now it's 2023 so this is going to be our input data and now we need to write a pattern from scratch that extracts the first name as a field or a capture group we also need to grab the last name as a capture group as well and then we'll have a third capture group that Returns the year from this kind of CSV is data so for starters we want a pattern substantially similar to what we just had so we'll say capital A to Z we just want one character because our first name is always going to start with a capital letter assuming that our data is clean and standardized then we're going to do a literal pipe character so we'll just say pipe here and the pipe has special meaning in the regx terminology so it's actually an ore so it's match this pattern or the next pattern or the following pattern and so what we want to do is escape that by using a slash here I'm also going to turn this into a raw string so that I don't have to escape the actual slash so if you just prefix your string in Rust with an r that will indicate to rust that this is a raw string and we don't want the slash to be interpreted as a special character from rust's perspective but the slash is now going to get passed into the regx engine and then the regx engine is going to ignore the special meaning behind the character that directly follows the slash so instead of using the special or logic inside of this regx pattern the regx engine is going to look at this pipe character as a literal pipe character which we have in our input data so following that pipe character we want to look for the last name so um of course I need to finish the first name here so we'll do a to z and we'll just do a quantifier of 2 to eight so that should match just fine because we have five lowercase characters so now we can do basically the same thing for the last name so we'll just copy that and put it for last name and then we want to detect the year and the year we'll just assume is always going to be four digits so what we'll do at the very end right here is do another literal pipe character by doing a slash pipe and then we'll look for 0 through nine and then do a quantifier of always exactly four characters so it will only match if there's four consecutive digit characters that are together at the very end here so now we want to turn this into from just a pattern to an actual capture group for starters let's go ahead and just run this I'm actually just going to get rid of this who went to the store prefix here and so we'll just do a cargo run and as you can see right down here on line 22 it is actually printing out the result I'll just prefix it with result so that we can explicitly see that it's line 2 2 that's printing that out and so it is actually matching right it is successfully matching but what's happening here is that the entire match is getting printed out so when you call find it's just going to give you the entire match without breaking it down into subgroups so how do we break things down into these child groups well first of all in the pattern itself we have to declare what a group is and to do that in the most minimal way we can simply surround each of these matches with parentheses so around the first name pattern that we have right here we're going to put a set of parentheses around that and that'll designate it as a capture group within the reg X engine the same thing so after the pipe character but right before we start the surname or last name we'll go ahead and add another set of parentheses and then same thing for the year right after the pipe character we want to put an opening parentheses and again at the very end we want a c closing parentheses so now we've declared that there's going to be three separate groups all the way from this one to this one and finally this one which is the year so now instead of using the find function here which just Returns the aggregate match with regardless of the capture groups now this is where we want to switch over to using the captures capability so when we go into the regex type here and use captures this is going to find the first match in the Hy Hy stack and what it's going to do is it's going to match each capture group in the regular expression so find doesn't care about groups but captures does care about groups captures is going to return this captures object and then inside of the captures object we can use the get function right here in order to return a capture group at a specific index now something that's important to note is that when you use this approach when you break things down into capture groups there is always going to be a capture group that's implied without you having to specify a capture group at position zero so you'll always be able to read the capture at position zero if you do have a match so the groups that we specified 1 two and three are going to be indexes one two and three so one will be first name two will be last name and three will be the four-digit year that we specified in this pipe delimited input data right so the zero with capture is always going to refer to the entire capture kind of like what find did but if we want those individual capture groups that we declared inside of our match expression that's where we need to go to index one two and three so what we're going to do is change the results here just a little bit instead of doing a find operation we're going to say dot captures and we're going to call captures on our input text variable and so now we have an option of type captures and we'll say if let sum uh captures equals match result then we want to do something with this captures variable so we'll go ahead and do a print line and we'll pass in a few different values so I'll just do you know value comma value comma value and then for each of these placeholders with these curly braces right here I'm going to pass in first name and then last name and then year right so we're going to parse those values out of the regex and then we're going to print each of those values independently because we captured them with these capture groups so for starters let's go into captures this variable that's populated with our actual capture object and then we'll say. get and again we want to get the capture group at position one because that's going to represent the first name field again the zeroth match group is always going to be the entire capture and so if we go into this match type here we can once again call this as string function to turn the match into a string representation so what we'll do is say dot unwrap dot as string so every time that we get one of these matches if we go back to the captures type here go to the get function every time that we call get on a capture group it's it's going to give us an option which could either be none or sum and so if it is sum then we want to unwrap it and then call the as string function to get the underlying value of that group so we'll pass in that one first and I'm just going to split this print line statement across multiple lines here to make it a little bit more readable we'll put a comma at the end and then we'll do captures doget number two so that's going to be last name. unwrap do as string and then finally we'll do captures doget 3. unwrap do as string so now we have one two three inputs for these three different sets of curly braces in our print line macro let's go ahead and do a cargo run here and sure enough you see we get Trevor space Sullivan space 2023 separated by commas because that's what we declared in our print line statement here if we wanted to do something else inside of our print line statement with those individual values it's going to Simply follow this pattern that we've specified here right but this proves that we were able to extract the first name the last name and the year from that pipe delimited input so even if I change these values to something else like Shannon Drew 2023 then if I do cargo run now we get different values for first name last name and year right so this is a really powerful thing with a regular expression engine to be able to take a single match but again break it down into individual components so that I can use those individual values that I've extracted from the input string and repopulate them somewhere else like maybe in a database in a certain uh fashion right so regular regular expressions are extremely powerful uh something else that you can do as well with groupings is to actually give the groupings a name so if I wanted to literally name this first capture group first name then I can use this little weird syntax here where I put a question mark right inside the first parentheses so in this capture group here I go to the first parentheses that opens it up and then I do question mark less than greater than and inside of that less than greater than bracket we just plug in the name of the capture group that we want so for this one I'll do first name and then for for the next capture group here I'll do question mark less than greater than and then I'll plug in last name and then for the last one here I'll just do question mark less than greater than and inside of that I'll put Year all right so let's change the year to maybe something else like 2011 and now we want to try to retrieve these capture groups by using their names instead of their indexes right so we could now kind of reorder things and put them into different position positions and we can always reference that specific capture Group by its name rather than just in a random index number right so to do that if we go back to the captures type right here you can see that we have the get function and this allows us to pass in the index right that's what we already used but if we go over to this function here called name this is going to return an option wrapping a match once again it's the same exact return type as the get function here but the difference with name is that we can return a match that's associated with a capture group that has a name but we can't use this until we actually give our capture groups names because until we name them they just have indexes so if you want to use the more friendly approach in my opinion where you actually name your individual groups and then use the name function this is going to be a lot more readable because now instead of doing do. getet one I can say name and then specify first name so now when I'm reading through my rust code here I can see that I'm specifically grabbing a group called first name somewhere in my regular expression so if I ever need to go debug my regular expression I know that this specific reference here is to the first name group and then same thing down here we'll change this to name and say last name and then finally we'll change this one from name from get to name and then we'll do year instead and everything else stays the same because the return type is the same as the get function all we do is replace it with a name call so now if we do cargo run you can see that once again those were extracted perfectly fine and we can substitute other values in here like Joe Schmo 2002 and if we run this again we extract those by their field names so by their group names here rather than just using a group index number so this is a really nice way to write regular Expressions again I would encourage you to go a step further and actually learn about different regular expression syntaxes I've just shown you some really basic regular expression patterns here but there's a lot more advanced things that you can do as well so hopefully you learned something new from this video if you did please leave a like and also just leave a comment below let me know what your thoughts on the video are and again please check out the pinned comment down below that has a link to my Amazon storefront that helps to support this Channel and we'll see you in the next video thank you so much for watching take care
Info
Channel: Trevor Sullivan
Views: 2,545
Rating: undefined out of 5
Keywords: rust, rustlang, rust developer, rust programming, rust software, software, open source software, systems programming, data structures, rust structs, rust enums, rust coding, rust development, rustlang tutorial, rust videos, rust programming tutorial, getting started with rust, beginner with rust programming, rust concepts
Id: 4W4uD99DlmI
Channel Id: undefined
Length: 44min 31sec (2671 seconds)
Published: Fri Sep 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.