‫Welcome back.
‫In this video, we are going to talk about regular expressions.
‫And it's a language or a pattern that the regular expression engine attempts to match in the input text.
‫So we can create input text and then we can use regular expressions to check for specific patterns.
‫And that's super powerful.
‫And it's not only used in C sharp, it's used in all programming languages pretty much.
‫And it's useful in order to find out, for example, if a user entered a correct email address or if
‫a user entered a correct link or stuff like that in general.
‫So you can use regular expressions in order to figure those things out and search for specific patterns
‫and do something.
‫If they're there or if they're not there, then don't do anything, for example.
‫And well, I am on this website where you can check out the regular expression language with a quick
‫reference, and there are so many different escaped characters in order to find stuff or in order to
‫create patterns.
‫And there are a lot here, and I'm going to use the most important ones of those.
‫I'm not going to go into every single one of them, but you will see many different examples which will
‫make sure that you understand regular expressions, and then it will be super easy to go back to the
‫side and use the other ones if you ever need them.
‫All right.
‫So let's go back into Visual Studio, and I have created a little sample text.
‫So let's create a new regular expression project.
‫I have one here.
‫I'll just call it any way you want and let's create a new file in here as well.
‫New item.
‫And this time we're not going to create a class, it's just going to be a very basic text file.
‫So here, text file and I'm going to call it sample text.
‫Now let's open up that sample text file.
‫And I just copied and pasted some code in there or some text in there.
‫And as you can see, numbers, telephone numbers, German telephone numbers and so forth, the alphabet,
‫some lalala text and so forth.
‫So we are going to use regular expressions in order to mark those specific patterns that we're searching
‫for, and that will make a lot more sense once we get into practice.
‫So let's press control F or command F in order to get to the find tool here.
‫And once you're here, press this, use regular expressions or alt e so that it will use regular expressions.
‫Now, now our search console here will apply regular expressions.
‫So what our regular expressions will use for some of them.
‫Let's start with a very simple one.
‫So I'm going to go ahead and start with tabs.
‫So let's go for backslash tab.
‫And as you can see now, tabs are marked.
‫So in my case, I'm going to make it a little bigger so you can see it better.
‫Here are some tips.
‫Let me create some more tabs.
‫As you can see now, the tabs are all marked here.
‫They're all highlighted.
‫Now let's find every new line by backslash n so you can see this is where the new line starts and here
‫as well.
‫So this is pretty much where I pressed enter.
‫Let me press some more enter.
‫As you can see here, each time I press enter and I break the line, I have this backslash n So that's
‫how I can figure out whether there was a new line or not.
‫So what do I get all of those backslash end and so forth from?
‫Well, I can find them here under the regular expression, language, quick reference, but I've also
‫created a little snippet of text file.
‫So let me create a little file and I'm going to paste it in there.
‫You can download them from the assets of this lecture and I'm going to call them snippets, dot text.
‫So here we have character escapes which are, for example, tabs, so we can find out whether there
‫are tabs in my string, which is in my example, text file, the whole file.
‫So here I'm checking for the whole file and I'm checking for every single new line or every single tab
‫in my whole file.
‫Pretty much.
‫Okay then.
‫We have the character classes, for example, a dot, a simple dot is a wild card matches any single
‫character except for backslash n so except for new line.
‫Let's check out if that's true.
‫So we have new lines.
‫Let's check it out again by using the N key here.
‫As you can see, we get all the new lines.
‫Now let's use the dot and dot, as you can see.
‫Matches any single character.
‫So that's what this dot and or dot does is a wild card.
‫So if you want to mark everything, well, that's the way to go.
‫So you can see it checks out everything it says except for the new line.
‫But as it seems, new lines are also marked.
‫So each time I create a new line, it's still marked with this dot.
‫All right.
‫So let's check out the next one.
‫I'm going to go through some of them and then we'll combine them to use some cool, cool things.
‫All right.
‫So matches any digital or any decimal digit.
‫Let's go ahead and check that out.
‫Backslash D standing for digit.
‫And as you can see, my number here, one, two, three, four, five, 67890 are all marked and here
‫all my numbers are marked and the values in between, for example, the plus sign, the hash tag and
‫so forth, they are not marked.
‫So any number will be found or any number will match that pattern of this regular expression.
‫Backslash d which stands for decimal.
‫Backslash capital d will do the opposite, so it will take everything else but numbers, as you can
‫see here again.
‫If you want to have the white space, you can use backslash s.
‫Then you get any white space that you have in your file.
‫So if you, let's say I want to get rid of any white space in your file, you could use this pattern
‫and get rid of everything that matches that pattern.
‫Capital s on the other side is everything that is not a white space.
‫So as you can see, the capital is pretty much the opposite.
‫Always.
‫So DX is all numbers.
‫Capital D is except numbers, so everything except numbers.
‫All right then let's check out w w will give me any word.
‫Can character and numbers are included.
‫So here it gives me all the numbers and all characters such as ABCD and so on.
‫So here the whole alphabet is marked as well as all of my digits.
‫Here you can see as well dots are not marked and slashes and those meta characters, those specific
‫characters, they are not marked as well and add neither.
‫All right.
‫If you want to have everything else, use capital W now if you want to have, for example, the LA,
‫let's search for la la.
‫As you can see, I can search it with lowercase or uppercase in both cases.
‫My la la la la la text here if you want to sing la la la la la la la and so forth, then that's the
‫one that is marked here.
‫But as you can see, all of them are matching that pattern, even though I'm not using the literal capital
‫letter or not.
‫All right.
‫And if I would like to only have the ones that are bound to one specific side, I can go ahead and use
‫Beck or D l a and they are so only the ones that have a boundary towards the left will now be displayed.
‫So which means that there is nothing before the LA or there is a white space before the LA, so only
‫those ones will be marked.
‫So as you can see, this LA is not marked neither this one.
‫That's because the word boundary is not on the left hand side.
‫So let's go back to the snippets real quick and check out what we have here.
‫So we have character groups which we need to look at.
‫Then we have the negation sign.
‫So the anchors, the match must start at the beginning of a string or line.
‫If you use that, then the double dollar sign and match must occur at the end of the string or before
‫a.
‫Next line at the end of the line of the string.
‫Pretty much.
‫All right, then.
‫Yeah, let's check those ones out.
‫Let's go back to our sample file, for example.
‫Circumflex.
‫And as you can see here, this one is marked.
‫So it's at the start of a string.
‫Let's go for an A.
‫As you can see here, there is the A at the beginning of the string or if you want to have an M, that's
‫at the beginning of the string as well.
‫So there you are.
‫What if I want to have the one at the end of the string?
‫Well, the thing is that we have a whole text.
‫So this whole sample text file is one string, pretty much.
‫So if I want to have it at the end.
‫As you can see, there is one character at the end of the string and that's this plus here.
‫So if I want to have the plus, by the way, or the DOT or any of those metro characters, I need to
‫escape those.
‫So if I search for them, I need to escape them.
‫And how can I escape them?
‫Well, I can escape them by using the backslash.
‫So backslash plus will give me the plus here.
‫As you can see, the literal plus on the other side.
‫So if I just enter plus, as you can see, this plus will not be found.
‫And that is because it's a meta character and we need to escape them if we use regular expressions in
‫order to find them.
‫So Backslash plus will give me the plus.
‫And as you can see, this plus here is marked as well as the plus there.
‫But now if I only want to have the one at the end of a string and as I said, this whole text is one
‫string, I simply use the dollar sign and now I can see this plus is marked.
‫However, the plus here is not marked or not highlighted.
‫Now let's look at range real ranges real quick.
‫So as you can see, we have this character group matches a single or any single character in a character
‫group by default the match is case sensitive.
‫So character groups.
‫Let's check them out.
‫Let's go into our sample text.
‫And in those square brackets, I can go ahead and do something like A to Z.
‫So now every single character between A and Z will be highlighted.
‫So as you can see, all characters are highlighted.
‫If I only want to have a to F, as you can see, only a f are highlighted and as you can see, it doesn't
‫care.
‫It takes capital letters as well as lower capital letters.
‫And so you can see our Visual Studio doesn't seem to care about this by default.
‫The match is case sensitive.
‫So in our case, the character group was not case sensitive, as you can see.
‫So it takes either.
‫All right.
‫But if you want to make sure and in other cases you need to use A to Z, for example, like that and
‫A to Z like that.
‫So they would have lower capital and upper capital letters?
‫Pretty much.
‫All right.
‫So that's one group that's an example of a group.
‫We can use any letter in here.
‫So as you can see, every single thing or every single letter that we put in there is marked as well
‫now or highlighted as well now.
‫So within this group, everything that is in the group will be highlighted.
‫Now we can create other ranges as well with digits.
‫So let's say I want to have every number, number between one and five that we are now.
‫I get all the numbers between one and five zero not included.
‫So one, two, three, four, five.
‫What if I want to have the opposite?
‫Well, then I can use the circumflex again.
‫So circumflex 1 to 5 will give me all numbers except for 1 to 5.
‫So if I want to have every single number except for the seven, let's say, what would I enter?
‫All right, I have.
‫I hope you figured it out.
‫It's just circumflex seven.
‫All right, how do I now figure out a specific pattern?
‫Because so far, we have used very generic, regular expressions.
‫We just used the very simple characters and so on and so on.
‫So what if I want to have this specific structure, let's say three digits, another three digits and
‫then four digits, and they are separated by the dash.
‫So how do I do it?
‫Well, I say it's one digit, then it's another digit and it's another digit.
‫Then as you can see, that fits for all those patterns that we have or all those numbers that we have
‫here.
‫That's because all of them are three numbers at least.
‫So if I add another one here, as you can see, the first line is highlighted.
‫Then here in our third line, only the ones with at least four numbers connected to each other is highlighted.
‫So what if I want to have this line three highlighted and line four as well because they are with the
‫same pattern, then I can do the following or the following thing like that minus backslash d, backslash
‫d, backslash d and backslash d So now I have those two lines or those two numbers highlighted.
‫This is a way how I can figure out all telephone numbers with this structure, and there are easier
‫ways to do that.
‫I don't have to enter so many D's if I don't want to.
‫There is a simpler way I can use the following approach so I can just say, okay, within curly brackets
‫I say home or I tell how many of the foregoing escape characters or patterns do I want.
‫So in this case, I say, okay, I want to have three D's, which means three digits.
‫The same thing goes for our second entry or second part of information.
‫And for the last one, as you can see, three is not enough.
‫We need four.
‫So now it fits those two patterns.
‫So what if I don't want only to have the minuses, but also the dots or also the hashes?
‫Well, I can either use that in both cases, but then as you can see, everything is included.
‫So as you can see here, we have three numbers, then we have something else and then we have a lot
‫more numbers.
‫So we have actually this piece here.
‫That's why the line seven is marked as well.
‫Same for eight.
‫As you can see, it starts after the 49, which means we have this pattern here as well.
‫But I don't want to have those patterns.
‫I want to be more precise.
‫I want to have, for example, the hash tag in there.
‫I want to have the minus and dot, but I only want to have this specific structure.
‫So how do I get that?
‫Well, I can use a lateral group search, which means instead of the dot, I'm going to use square brackets
‫again and they will be my little group search.
‫So in here I say either it's a dot.
‫Or it's a hash or it's a minus.
‫So as you can see now, all of those three characters are fine.
‫But nothing else.
‫Now, if I want to use it for the first character here as well.
‫So I had a dot here.
‫So it took the minus, the dot and the hash.
‫But I want to have all three of them and not anything else.
‫So what if I enter an A here, for example?
‫Well, now it's not the case anymore.
‫But if it's just a dot, then the A is fine.
‫But if I enter my group that I had there, my specific little group search.
‫So this is a little group search here.
‫Now, it's not included anymore, so it has to be one of those three characters.
‫All right.
‫If you feel like a challenge and you're up for a real challenge, then create the structure for all
‫those three numbers.
‫So create a pattern for all those three different numbers, because these are three different ways of
‫creating a mobile phone number in Germany.
‫And that's the general structure.
‫So this is if you call from Germany, this is if you call from outside of Germany and that one as well.
‫So either a plus or double zero, that's generally if you call someone outside of your country, you
‫need to have the plus plus the country code.
‫And it's the same with zero zero, which represents this plus.
‫All right.
‫So if you want to, you can create the structure for this one.
‫It's pretty difficult.
‫And I still need to show you one thing before you can do that.
‫And maybe it's a little more than just one thing.
‫So, first of all, let's go ahead with the pipe.
‫So let's say I want to have plus 49 or I want to have something like zero zero 49.
‫Now, that by itself won't work.
‫Why is that?
‫Well, if we look at the snippet, we can find qualifiers and we have not used qualifiers yet.
‫And that's something that we will need to do now.
‫The same thing for grouping constructs and alternation constructs.
‫So these are the ones that we are going to use now.
‫We need to check those out before we can go ahead and create the whole structure for those numbers.
‫So let's start off with using alternation constructs and qualifiers.
‫So the plus says matches the previous element one or more times.
‫Well, I don't want to do that.
‫I just want to have a literal plus.
‫And as you know, a plus is a meta character and it needs to be escaped.
‫So what I need to do before the plus is a backslash.
‫And now you can see I'm using the pipe here.
‫So I say either it's plus 49 or it's zero zero 49.
‫And as you can see, both those structures or both those patterns are marked here or both my numbers
‫here are highlighted.
‫So that's what this pipe does.
‫It's either or.
‫So one of those has to be true in the whole number that we're looking at.
‫Then it's fine.
‫And that's not just for numbers, that's for characters as well.
‫So, for example, if I want to have Mr. and Mrs. so if I want to tag those, I use M R pipe M's, which
‫means either Mr. or Ms. have to be true.
‫And it's the case here in our case.
‫So that's the either or, as you can see here, our alternation construct.
‫Then the grouping construct is something that we need to look at.
‫And in order to look at those, I think it's best if we look at the links here.
‫So we have certain links here, HTTPS and HTTP links and either with W, W, W or without.
‫So we have four different links here.
‫And if I want to use a group, let's say I want to use this group w w w but as you can see, it's not
‫the case for all links.
‫So in order to find the links, we need to first of all go to HTTP s, then we have this structure.
‫So far everything is good.
‫And now if I want to have those two still active, I need to check out this group w w w because here
‫it's the case and here it's not.
‫So how do I do that?
‫Well, I need to use a group, so I'm going to group w w w and as you can see so far only this link
‫here.
‫The first one is highlighted.
‫But then if I enter this question mark, then it's fine.
‫So I'm using the group w w but I'm combining it with.
‫The question mark and what does this question mark do?
‫Well, it's a quantifier and it matches the previous element, zero or one time.
‫So the qualifiers are the star, which matches the previous element zero or more times, then the plus
‫sign, which matches the previous element one or more times.
‫And then we have the question mark, which matches the previous element zero or one times.
‫You can also say how many times you want us to be.
‫Exactly.
‫So it matches the previous element.
‫Exactly.
‫End times and for a range as well.
‫So at least end times.
‫But more than or but no more than MX times.
‫So that's the different quantifier that you can use.
‫So now you've seen one example of grouping, so grouping the W and then one example of qualifiers by
‫using the question mark.
‫So as you can see, both things are still active.
‫So this w w w and the one without is still active and here it's actually w w w dot.
‫So this dot is included as well.
‫And then after the question mark, I can go ahead and say tutorials dot eu.
‫So both things are active.
‫Okay.
‫So let's go back, though.
‫We still have this little challenge to get all of those numbers to be active.
‫And by now you should be able to do that by what you have seen.
‫It's pretty tough.
‫I'm telling you, it can take quite a while, maybe even half an hour or so.
‫Even though it's a very basic example, it can take a while to figure it out based on what you've learned
‫or based on the snippets that you've seen here.
‫So it's maybe best if you download the snippets file and look at the different character classes, character
‫escapes, anchors and so forth to try it.
‫That's, as I said, a difficult one.
‫If you don't feel like a difficult challenge, then wait for the easier challenge, which will be to
‫find all website links or to well, mark all website links or highlight all of them.
‫All right, so now you should know everything that you need in order to create patterns to find German
‫mobile phone numbers.
‫And there is one thing to consider.
‫So they always start with a zero or a plus 49 or a00 49, then follows a one.
‫So that's always the case.
‫So as you can see, a one here, another one there and one here.
‫The following numbers either is six or seven, so there are no other numbers allowed here.
‫Then the next digit, which is in this case the fourth digit can be any number.
‫Then you have a slash and following are just eight numbers.
‫And those eight numbers can be any number, so it can be any of those ten different digits.
‫So that's generally the structure.
‫Please try to build that.
‫If you feel like a real challenge, it is not easy.
‫It can take quite a while to set it up.
‫But you have seen what you need to do that and well, please just give it a try and go ahead and check
‫out the snippets to do so.
‫So always go back to the snippets in order to figure it out and it can be difficult.
‫And if you feel like you're not making any progress and you can't get it to work so can't get all those
‫three numbers to be highlighted, then that's fine because I'll explain just after.
‫All right.
‫So I'm going to explain now, if you want to try it yourself, just pause the video.
‫Okay.
‫So first of all, we have either a zero, a plus 49 or a00 49.
‫So let's start off with the plus 49 and I'm going to use a group for that.
‫I need the backslash plus 49 in order to use the the literal plus, as you can see here.
‫Then the alternative is a0049.
‫So it's either plus 49 or it's 0049.
‫And then the other alternative is it's just a zero.
‫So as you can see, it's just this zero here.
‫But as you can see, all the others are now active as well.
‫All other zeros are active as well.
‫And I'm going to add a question mark here, because I'm saying there can be a zero or there can be none.
‫So in those cases, there are none in those two cases.
‫Only in that case, we have a zero.
‫So now let's check out what this question mark means.
‫It means matches the previous element zero or one time.
‫So zero in those two cases and one in line eight.
‫Okay.
‫That's not it.
‫That's just the beginning of the number.
‫Right.
‫Follows a one.
‫So now we have zero one and then a six or a seven.
‫So I create a group again, just going to say six, five, seven within my brackets.
‫So so far everything is good, then it can be any number.
‫So now I can just use backslash D, then we have a slash and then we have another number.
‫And it's not just one, it's eight digits.
‫So we can just say, okay, it will be eight digits and that we are.
‫That's the structure.
‫I'm going to add it just below here.
‫So that's our pattern.
‫In order to get German telephone numbers, well, at least the ones that we see here and in general,
‫you should find all German telephone numbers.
‫Now, you could, of course, go ahead and try the same thing for your country numbers.
‫So whatever you're from, wherever you live, check out the telephone numbers or the mobile phone numbers
‫and see how the structure of those mobile phone numbers is and try to build a pattern for that.
‫You can even share that if you want.
‫That would be interesting to see for everything.
‫Everyone else in the course and me myself as well.
‫It would be very interesting to see where you're from and how the structure of your countries telephone
‫numbers or mobile phone numbers is.
‫All right, great.
‫So I hope you manage to do that.
‫And if you didn't, I hope you could follow along the structure that I've just explained.
‫Now, let's go ahead and well, actually, let's check out.
‫The Mr. and Miss example again, because here we have either Mr. Panetta or we have Mr. Mueller, without
‫a doubt.
‫So how can I check out if the DOT is there or not?
‫Well, by now you should know.
‫You can use a question mark for that.
‫So let's just start with Mr..
‫As you can see, all three or all four lines are marked at least the.
‫Mr..
‫And then if I use a dot, you can see that all of them are active as well.
‫But that's because that means wild card or means anything.
‫So it could be empty space here as well.
‫So if you want to make sure it's a literal dot, you need to use the backslash.
‫Now, the next one is an empty space and afterwards we have the name.
‫So it could be A to Z.
‫So anything A to Z like that.
‫So now how do I get Mr. Mueller and Mr. Robertson in there as well?
‫Well, I need to use the dot again, but this time with a question mark, because what I'm telling now
‫is there can be a dot order can be done, and afterwards we have an empty space.
‫So here I use the literal empty space to get that, but you can as well use backslash as because backslash
‫s if you check it out again says matches any whitespace character and that's what we wanted.
‫So now all of those names are correct.
‫And if you want to have a specific amount of characters following, so the name should be at least four
‫characters, then you can use this quantifier here.
‫So we are using the quantifier with N being four.
‫So we say it's at least four characters or four letters.
‫If you wanted to not only have characters like A to Z in there, but also numbers, for example, then
‫you could use backslash W So backslash W just means it's a as you can see here, it's a word character.
‫So A to Z, A to Z, 0 to 9 and underscore.
‫So it's useful for, let's say, player names or usernames, so stuff like that.
‫And if I want to have multiple characters, I can use the plus sign.
‫So the quantifier plus matches the previous element one or more times.
‫So let's check it out.
‫Let's add the plus.
‫And as you can see now, Mr. Duda, Mr. Muller, Mr. Robertson and Mr. G.
‫All are fully highlighted because, well, we have said that we want to have one or more characters
‫and if you wanted to have zero or more times, then we could use a star.
‫As you can see, the star works here as well because we have more times or more characters.
‫All right, now it's time for a little challenge.
‫Find a website link.
‫So we started off already, but now please go ahead and find every website link that's with this structure.
‫And it doesn't have to be tutorials.
‫EU It should not be a literal name, but it should be generally the structure and the structure is,
‫as you can see, https or HTTP colon, slash, slash.
‫Then w w w dot or not.
‫Then the name of the domain and the domain and the top level domain, which is EU in this case.
‫So it's separated by a dot, of course, as well.
‫All right.
‫So please go ahead and try that.
‫All right.
‫I hope you tried it.
‫So we start off with http sx and the SX is optional.
‫So we just enter the question mark.
‫So it has to be a yes or no as that's what this question mark does.
‫Then we have colon forward slash, forward slash, and then we have a group.
‫So we have a W WW DOT group which is optional.
‫So we use the question mark again, then we have the name.
‫And in this case I'm going to use a word group with W plus and it has to be backslash W here of course.
‫So now it's using the domain name as well, followed by a dot, which could be a literal dot, pretty
‫much so let's use a literal dot within the next group where we also have another word character.
‫So it can be one or more word characters and that's it.
‫So that's our structure.
‫So this one here, plus one or more times, which we have two times.
‫So the high level domain is this piece here with the dot before that.
‫So backslash dot, which means it's a literal dot that we have there.
‫Then we have the word itself or well, in this case it's EU or it could be dot com or it could be dot
‫academy or stuff like that.
‫Then we have the name itself, which is this piece here.
‫So the, the name of the domain w w w or not and http or https.
‫All right.
‫So you can create your own regular expressions, of course, but sometimes it just makes sense to Google
‫them.
‫So let's say you want to have the expression for email addresses.
‫So you could, of course, go ahead and say, okay, I want to have characters before that.
‫So you say W plus then followed by an ad sign again W plus followed by a literal dot like that and you
‫have again something like com whatever.
‫So again a backslash w plus.
‫So that would be a very, very generic thing, but it could be pretty fake as well.
‫So think people have thought about very, very complex regular expressions which really are pretty secure.
‫So let's check out the Internet for that.
‫So let's go back to the Internet and now let's Google for email address reg X or reg X regular expression,
‫and then you'll find this website.
‫It's called email regexp and it says email address, regular expression that 99.99% works.
‫And here you can find the regular expression, the official standard, then the Python version, the
‫JavaScript version, HTML and so forth.
‫So as you can see, the different programming languages here in this case.
‫So that's the one for C sharp.
‫But as you can see here, another way to use the system that met dot mail, dot mail address class.
‫So you can either use this expression or you can simply use the class.
‫And I would usually use the class, I guess, to determine whether an email address is valid.
‫Pass the email address to the mail address, dot mail address, string class constructor.
‫So that that sounds like a valid way as well.
‫But generally that's how you could do it.
‫So if you want to have a regular expression, you can very often search for it.
‫Using that one, however, and copying it into my Visual Studio file finder actually doesn't work.
‫So let's use the standard one.
‫So the general email regex.
‫So I'm just going to copy that code here and go back to Visual Studio and now search for that specific
‫regular expression.
‫And as you can see, test the test dot com does work if I use info at google dot com.
‫As you can see, it works as well.
‫That code seems to work as well and so forth.
‫So there you are.
‫That's a regular expression.
‫And if you would have to, well, figure this one out by yourself, I think you would.
‫You need quite a while, but that's the whole point, right?
‫You don't need to develop everything yourself and think of everything yourself.
‫You can save hours and hours of your life just using the Internet to figure things out, just using
‫Google to find what you're searching for.
‫Very often you'll find a solution which works pretty well, and you don't need to create all of it by
‫yourself.
‫All right.
‫So I hope you liked this short course to regular expressions.
‫And there's one last thing to do, and that's using regular expressions in C sharp.
‫And I know this video is pretty long, but hang on because now we're going to use C sharp to use regular
‫expressions.
‫So in order to use regular expressions, you can go ahead and create a regex.
‫And in this case, as you can see, it doesn't know regex.
‫So we need to add the namespace, which is system dot, text dot, regular expressions.
‫That's the one that we want.
‫Now we can use regex and I'm going to call it regex and this will be a new regex.
‫And here I can enter the pattern.
‫So you can as you can see, it's a string pattern and you can either put it in here or you can create
‫a string pattern, which will be your pattern.
‫So let's say in this case, we need to use the ADD at the beginning and I'm using backslash D to find
‫all numbers in my pattern.
‫So I'm going to give it to the constructor of the regex class here.
‫And as you can see now, it's going to use this regular expression pattern, which is in my case, just
‫the digits.
‫So it's just going to say, okay, give me all digits.
‫Now, let's create a test string and I'm going to call it text and that will be hi there.
‫My number is one, two, three, one, four.
‫So that's going to be my test.
‫And now in order to find every single character or every single time that this pattern fits, I'm going
‫to use a for each loop because I want to show all the different matches that there are.
‫So let's use a match collection for that.
‫Match collection, if you check it out, is a type which is not valid in the given context.
‫Well, that's fine.
‫Let's create a match collection.
‫I'm just going to say match collection is equal to reg x dot matches and the matches method.
‫What will it do?
‫Well, it will take an input, which is my text, and it will try to fit it with my pattern.
‫So we're using this regex here.
‫This is the pattern.
‫We gave it to the regex.
‫So now it's trying to match the pattern to the text that we have.
‫And the text is this line of code here.
‫So what we can do now is show all the different hits.
‫So I'm going to say something like so many hits found and I'm going to use a new line for that.
‫I'm just going to say like this.
‫So this is the amount of hits that I have and that will be my match collection dot count.
‫So as you can see, this match collection object has a property which is called count and it will just
‫say, how many times did it find whatever it tried to find?
‫So whatever the pattern was and whatever the text was and I'm just going to say text here as well.
‫So I say within the text, I have this many hits and now let's show the actual hits.
‫So I'm going to use for each and it's going to be a match which is called Hit in Match Collection.
‫So with this for each loop, I'm just going to loop through.
‫The whole match collections or head match collection, which contains all the different hits.
‫And I'm going to print every single hit on the screen in order to also see at which point in the string
‫it was.
‫So which position within this string it is.
‫So let's say it has I don't know, it's like eight or seven characters here.
‫Eight, nine, ten.
‫So it's around, let's say 2020.
‫Is this 121 is this 122, is that one?
‫And so forth.
‫So if you want to have those as well, we can use a group collection which will be my group and it's
‫going to be hit dot groups.
‫Next I want to display that.
‫So see w.
‫Double tap time.
‫And then I say, okay, I want to have the hit itself.
‫So I'm going to use a curly bracket found ET and the position.
‫So how do I get those?
‫Well, I use the group that I have created here.
‫So this group collection and I'm just going to say group dot or actually in square brackets at the position
‫zero.
‫Give me that and give me its value.
‫And then I also want to have the index.
‫So Group zero and index.
‫All right, now let's run it.
‫And what it will do is it will give me the value and the index where it was found.
‫And I'm going to start with a control f five so it stays open and it says one found at 23 to found a
‫24 three found at 25, one at 26 and four at 27.
‫So as you can see, it has found digits and it has given me the position of those digits.
‫And the position is the position within the string that we have here.
‫All right.
‫So if I have a different pattern, let's say I have this pattern of multiple DS, so I want to have
‫five digits.
‫So let's check it out again.
‫And now it should only find one digit or one entry and it says hi there.
‫Number is one, two, three, four, three, one, five, four.
‫And here, one, two, three, one, four, found at 23.
‫So whatever you have set up here that's going to be found and if I search for there, then it will give
‫me this there which starts at index three.
‫So I'm going to start it and we are.
‫Hi there.
‫My number is one, two, three, four and hi there.
‫Found at three because this is the third position of a three position here index three of our string.
‫Okay.
‫So that's an example of how you can use RegEx and C-sharp.
‫There are more.
‫You can check them out.
‫You can just check out the documentation for that.
‫But this video is more about the general idea of regular expressions and how to use them.