Welcome back.
In this video we'll implement regular expressions into C sharp.
You will see how to create and use regex objects.
And we'll walk through a practical example together.
All right so let's get started.
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 in here I can enter the pattern.
So 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 at at the beginning And I'm using backslash D to find all
numbers in my pattern.
So now 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 a
digit.
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 12314.
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 regex 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 okay, 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 at which position within this string
it is.
So let's say it has, I don't know, it's like 8 or 7 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 CW double tap sign.
And then I say okay I want to have the hit itself.
So I'm going to use a curly bracket font at 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.
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 F5.
So it stays open.
And it says one found at 23, two found at 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 12343154.
And here 12314 found at 23.
Okay.
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 1234.
And hi there found at three because this is the third position or three position here index three of
our string okay.
So that's an example of how you can use regex in 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.
And that's it.
You now should have a solid understanding of regular expressions and how to use them in C sharp.
I hope this was easy to understand, but in any way, if you have any questions or feel like you are
missing something, remember to ask in the Q&A section.
You will find us in there answering your questions.
All right then, see you in the next video.