BrandGhost

C# Regex On EASY Mode - 3 Beginner Examples

Have you used regular expressions in C#? If not, no worries! This is the perfect beginner primer to get you started using regex in C#! Regular expressions allow us to do more complex string pattern matching. This means that we're able to do more advanced variations of the basic C# string checks like StartsWith and EndsWith! Check out these beginner regex examples in CSharp! Want to see the benchmarks? Check 'em out! https://youtu.be/nw8B_E5ICMM
View Transcript
When developing applications it's really common that we need to be working with strings and looking for patterns within strings as well now in C we have lots of different things built in where we can go look for things that start with a given string end with a given string or even find different positions within a string where another substring exists however when we need to start working with more complicated pattern matching we have to switch away from this and go over to something called the regular expression hi my name is Nick centino and I'm a principal software engineering manager at Microsoft in this video we're going to look at regular expressions or Rex in C now regular Expressions can get incredibly complicated very fast so I'm not going to do a super deep dive on all of the really ridiculous things that you can do with the Rex but instead I'll cover just the basics see some examples that you can extend and how you can apply them in code a quick reminder to check that pin comment for a link to my free Weekly Newsletter and my courses on dome train with that said let's go over to visual studio and check this out in Visual Studio here I have three different examples that we're going to look at and the first two are very similar to other string Methods that we already have built in but instead we're going to be able to compare and contrast how these look we'll start off with the starts with example and this is going to be very similar to what we have built into the string method so if I had a string that had anything in here like hello I could do starts with directly on here and we could check to see if there's another string that existed at the beginning of this string hello but instead we're going to use a regular expression to express the same type of thing here so in this example I have a list of input strings we're going to talk about these in just a moment but these are what we're going to be testing out and if we look a little bit lower from line 23 to 24 we have a pattern and a regular expression is based on a pattern that we want to match against and then I create the new Rex instance I'll finish walking through this example but I want to show you a couple of different variations of how we can do this part in particular and the same code on line 28 and that's the other interesting part of this example which is calling the match method on the regular expression that we've created so once we have a Rex what we're able to do is try matching it against the different input strings and in this example we're going to go against these four input strings that we see here from line 17 to 20 having a closer look at the pattern that we have here on line 23 we can see that it says hello but it also has this special character at the beginning and this is just a carrot or a hat character right it's a little arrow that's pointing up and for regular Expressions this is going to indicate that it's the beginning of the string that we want to check against if you take that piece of information and you think about the other part hello here that we have when we go to do this match operation it's going to say that we want to match hello against the beginning of the string that we have and if we have a quick peek at the different inputs that we have here we can see that line 17 that starts with hello line 18 does not so that shouldn't show up in the match 19 looks like it might but there is some white space here and regular Expressions do care about white space there's different things that we can put in place to either ignore white space or include it in the pattern that we want to match and then line 20 also includes hello so I would expect the text on line 17 and 20 to match in this particular case but I did mention that I wanted to show you a couple of different ways that we could express the code that's from line 23 and 24 as well as 28 so we can do what we see here on line 24 where we create a new Rex instance but there is also a static method on the static class called Rex where we can say match and we can do something very similar here where if we put the input which if we look here uh looks like co-pilot's putting the wrong suggestion so be careful when you're using co-pilot it's the input that we want first so if I were to put something like hello here uh and then the pattern after this is technically What It Wants we can use this static method instead of creating a new Rex instance functionally these will do the same thing where they match against the patterns however there is different performance characteristics of creating new Rex instances as well as using the static method in a future video which I'll link at the end of this one we'll look at the different ways that we can use the Rex instance the Rex static method as well as some other options and the performance characteristics that go along with them for now if we go run this we can go check the input and see what matches looking at the output from our example we can see that hello world did match the string right so that's because the hello is at the very beginning we can see something something hello did not match and that's because again hello was not at the start if we look at the third example when we have the white space at the beginning like I said that is at the beginning of the string right the white space not the word hello so it won't match and then this final one does also match as well so the first and the last one do match because low is at the start I should also mention that by default regular Expressions will operate case sensitive that's important to note because you do have full control over how you want that to match you can either change the expression that you've created yourself to be able to handle upper and lower case or you can add in Rex options which will allow you to change that so for example if we wanted it to match and not care about the different casing we could add these Rex options and add ignore case just like this here that we see on line 24 now that would mean that if we had something like this this would in fact now match because it is case insensitive switching over to the next example which we can do a little bit quicker we can see ends with which is basically the exact opposite of what we just looked at right so when we're talking about something ending with instead of putting the carrot character at the beginning of the pattern that we want to use we use the dollar sign character and in a regular expression the dollar sign character denotes the end of the string the setup for this is the exact same so I'm not going to spend a ton of time on it I just wanted to show you that we can go run an example like this so if we look at the inputs here if we look at the pattern that we have as well we're looking for words or strings in this case that end in ing so ing has to be at the very end because that's what the dollar sign means we need to be matching against the end of the string so coding should in fact match because ing is at the very end of the string if we look at I look programming that's not going to match because there's an exclamation mark at the very end right that's the last character in the string not ing same thing on line 43 there's an exclamation mark here coding is somewhere near the beginning of the string so definitely not a match and this fourth example programming is at the end unlike the second example there's no exclamation mark right so it should in fact match now I won't waste our time running this one because it's very much like we just saw except the beginning versus the end of the string up until this point you might be saying well why the heck would I ever use this we already have string starts with and string ends with and you're totally right because for these examples I wasn't doing anything fancy at all with the regular expression we were just looking for characters that were very straightforward right even if you wanted to use a case and sensitive comparison you could do that with starts with and ends with built into the string method but where regular Expressions get really cool is that you can start looking for different patterns you can start having things that are variable within the pattern and not just a hardcoded string so what we're going to look at next you could indeed apply to the first two examples we just saw with the starts with and ends with but instead we're going to be looking for Strings anywhere in another string but they're going to be a little bit more variable so let's go check out this example I wanted to change this one up a little bit too because what we were seeing before is when we were calling for match what happen is we would get a match object back and we could inspect that to see where that match occurred within the string kind of like giving us some information about that input string compared to the pattern and the location that it was matching at so you could go inspect that to get some extra details in this case if you just wanted to do something like a contains right if we use that built-in string method this is match is going to be very similar to that type of thing but instead we're not going to use hardcoded strings like we saw in the first examples instead we're going to look for a pattern so this should look a little bit more interesting here I'm going to skip over these inputs for just a moment and if we look at the pattern that we have on line 75 you can see that this looks a lot more complicated than what we saw in the beginning this pattern is pretty straightforward but if you haven't seen a regular expression before it probably looks pretty wonky I also mentioned at the beginning of this video that I don't want to spend a lot of time deep diving on all of the different things you can do with a regular expression in fact I actually find that chat GPT is pretty good at giving you regular Expressions that you want to match against but there are online regular expression testers you can use I like Rex storm it's a pretty goodnet one and you can go run your regular Expressions right in your browser and test them against inputs so a nice combo that I like to use is asking chat GPT hey can you match against these strings for me and give me the regular expression that would do that and you can ask to give it to you in C and it will give you a string that you can copy and paste very nicely because some of the characters that you use in a regular expression are backs slashes and that can really mess stuff up inside of quotes you can take that put it into redj storm and then match against inputs and test it out super handy combo when you go to drop it into the code you have a pattern like this let's say I'll walk you through what this one does and it's going to start with this part inside of the square brackets where we have 0 to 9 and a plus sign after so inside of the square brackets it's saying match any character that is the number 0o through 9 and then the plus sign after that means at least one so if we have a string that has at least one we will continue trying to match the next part here again using the square brackets this is indicating like a range of characters that we're allowed to match on so you can see that I have lowercase A through Z and then upper case A through Z as well so this means that it will match on characters in these ranges because it's inside of the square brackets it will match on one of the characters in here but this asterisk after instead of a plus sign that's going to mean zero one or more so in fact it means that anything here could be a match that's kind of interesting right now if we look at the end it's very much like what we have at the beginning in fact it's the exact same it's just that it's following this other pattern that means that we need to have at least two numbers that's exactly what this pattern suggesting we need to have at least two numbers because if we look at the beginning there must be at least one number here there must be at least one number at the end and there has to be something or nothing inside of here you'll also note that I don't have a carrot at the beginning and I don't have a dollar sign so technically the numbers that we're looking for do not have to occur at the very beginning or the very end but when we look through these examples I think I might have coded them up that way so it's just a coincidence jumping down to line 80 very quickly you can see that the is match return value is just a Boolean so this is just going to be true or false unlike the other examples that Hann a match object come back you can still use match here if you want there's also matches plural which will give you all of the matches in a string if they exist but I'm just showing you a couple of different variations so you're exposed to this to play around with it looking at the input list I do have the comments after for what should match if we have a quick scan through Nick should not match because I did say the minimum requirement if we have a quick scan through the pattern is that we have to have at least two numbers there's no numbers in this first one this second one there's still only one number the third one still only one number and the fourth one this is the first match we're going to get now what's kind of peculiar about this is that if we look at this part here this pattern looks like it's supposed to have a number followed by some characters followed by other numbers and if you look at this match here this one's saying we have some characters followed by two numbers so it's kind of misleading because you probably want to feel like the letters need to come between the numbers but remember I said that this will also match nothing and because we're not anchored to the beginning or the end it's actually matching these two numbers at the very end of this string so it will say like for n not a match I is not a match C and K are also not matches so this part none of it matches right but four this character here matches this first part of the regular expression and then it's able to match nothing and then another number after it for the last part of the pattern so you can see that it gets complicated pretty quickly but this is just how regular Expressions work and if you again are not used to them spend some time playing around with them use an online Rex tester because there's a lot of different variations you can play with and I assure you that when you're not used to using it uh it's pretty frustrating and it can be pretty confusing but it takes some time to work with the patterns and just to kind of prove the behavior here I ran this example we can see that the first few so first three and in fact don't match kind of like called out in the comments here we can see that all of the ones with two numbers do match so there's a 42 a 42 a 4 and a two it doesn't matter if they're beside each other or not as long as there's two all the way through until the last ones except the very last one because there's only a single number we do in fact need two numbers so that's a quick look at how we can use regular Expressions to do pattern matching Beyond just simple hardcoded strings to see if they exist within other strings you can use reject to replace things like starts with ends with contains or even index of because the match object does have the index and in fact if you wanted to find multiple matches instead of doing index of in a loop and trying to find all of the matches you can use a regular expression to find all the matches at once now in the third example we saw you could already see that if you're not used to using Rex patterns they can get pretty complicated pretty fast and that's okay they do take some time and practice to learn I'm certainly no expert and I always use an online Rex tester to play with them because I'm guaranteed that I'm going to miss some scenario if you recall earlier in this video I did say that there were performance characteristics that were different between the various ways that we can create a Rex and do some matching so if you want to see that you can check out this video next thanks and I'll see you next time

Frequently Asked Questions

What are regular expressions and why would I use them in C#?

Regular expressions, or regex, are a powerful tool for pattern matching within strings. While C# has built-in string methods like 'starts with' and 'ends with', regex allows for more complex pattern matching, enabling you to find variable patterns within strings that go beyond simple hardcoded comparisons.

How do I ignore case sensitivity when using regular expressions?

By default, regular expressions in C# are case sensitive. However, you can modify your regex pattern to ignore case by adding specific options. For example, you can use regex options to include 'ignore case', which allows your pattern to match regardless of whether the characters are upper or lower case.

Can I test my regular expressions before implementing them in my code?

Absolutely! I recommend using online regex testers like Rex Storm to test your patterns against various inputs. This way, you can experiment with different regex patterns and see how they behave before integrating them into your C# code.

These FAQs were generated by AI from the video transcript.
An error has occurred. This application may no longer respond until reloaded. Reload