Hello Welcome to
Another regular expression video can you believe it here we are again again talks about regular [expressions]. I can't believe it
This is my life. Isn't it anyway um
Okay, so in this video. I want to talk about something called capturing groups and back references kind of ominous weird sounding
But they're very powerful very interesting and you can use them
This is really where you start to get into exciting steps you can do for experimentals strange regular expression poetry stuff, okay?
So what do I mean by capturing groups, so let's say let's go back to the phone number
[let's] go back to the phone number example, so let's say I have these two phone numbers to 12.5
5 5. 1 2 3 4 and I have 9 1 7
and I'm just going to use the dash here for a second -
- 8 6 7-5
309 so let's say ultimately what I want to do is I want to redact these numbers
So I want to still see the area codes
But I want to
do a search and replace any phone number and
I want them to come out like this so what I need to first do as we know is match
These phone numbers and the way that I can match these phone numbers
Let's we've done this a bunch of times, but I'll write it over Here d 3 digits
followed by a dash followed by 3 digits followed by a dash followed by four digits
[ok] so here's the thing
Whatever you do a regular expression search
It matches a particular. It doesn't match right. This is the whole thing that is matched
And I'm going to erase the second one just to give myself some more room here
You can up, and then what it automatically does is assigns this full match to something called
group number [zero]
so I have captured this match from my regular expression, and I've called it group number 0 if I put
[parenthesis] no [parenthesis] can be used for alternation doing it
But parentheses whether or not they're used for alternation or not also will capture a subgroup so in other words
What if I put in open parentheses here and obviously I'm making this very extreme and a [closed]
parentheses here what that means is this part of the full match should be captured separately or additionally as a
Different group and this now is group
number one, and what if I do this over here this group now is
group
Number two, and I'm having running out of room here group, two
Sews behind the scenes the regular expression has done this match and stored in the computer's [memory]
Three separate strings the full match is group 0 the first
You know from left to right the first sub match
Captured parentheses as group 1 the second sub match captured parentheses as group 2. I don't know what I put dot
This [is] dotted lines. Those are full parentheses, so this is very powerful
Why?
Because what I can do is in
Different ways I can refer to those groups whenever I want to and there's two ways of referring to those groups
for example dollar sign one refers to group number one but also
Backslash one can refer to group number one and these are being used in different places
so the dollar sign is used whenever you're doing a replace and you want to refer [to] a captured group and
Backslash one is when you're doing a
When you're within the regular expression itself referring to a captured group
So I'm now going [to] show you two scenarios where each of these is used okay, so hopefully this makes some sense to you
But it'll make more sense once I actually play it out here
So the first scenario is let's again
Apologize for this let's again write the regular expression to match all three of these phone numbers
And if you recall actually I can probably scroll up and find it never mind. I can say I want to have what and
Optional open parentheses followed by three digits followed by a dot a dash or a closed parenthesis
followed by whoops
[what] did I miss here?
Range out of order. Oh the dash has to be first in
the when the character left followed by
Three digits followed by again a dot or a dash or a dot dash has to be first and then followed by four digits
So now I have now matched all four of these groups, so let's say what I want to do is keep
only the first number and
Then I want to redact the second only the first three numbers
I know the area code so to speak and I want to redact the rest of the phone number
So the way, I would do this is let me capture only the area code, so this by the way this these three numbers
right there, that's the area code so what I can actually do is put parentheses [around] it and
Now what I want to do when I do my replace right? This is the replace is I want to say hey let me replace
it with whatever was captured in group one followed by - x x x followed by - x [x] [x]
Now I'm going to write, so this is saying like use the thing that I captured in the replace
So let me show you this and now I'm going to do I'm going to do a replace right replace
Look what happened there replace look what happened there replace now
I lost the original
formatting of the regular expression because remember these are a lot of I don't know why it's what Format it thinks I am but
Remember, I'm not I could now this could be an exercise
I'm not going to go through this right now because I'll go on for too long but an exercise you could try to try is
Can you capture?
The rest of the formatting of the phone number and then have multiple groups so it still retains right? It's right now
I'm not retaining. It's all the phone numbers now
I have this format
but if I undo right
I want to keep the I want to keep the dots or [the] dashes and the parentheses
Try to rewrite the reg expression to do that very interesting exercise to try to do okay
Let me think of a slightly [different] scenario. [I'll] show you this can also be useful. This is how to me before
I'll show you
Let's say I wanted to find and replace
Actually, I have a first name comma last list of first names common last name I
Can't think of any other names
Okay, so you can see I have a bunch of names first name comma last name sorry last name comma first name and I want
To redo this list to just make this list first name comma last name
It's like I have thousands of these I want to use a rag pression
I don't want to have to sit here and be [like] cut
paste
space
Delete right
I could do that for every single line or I could try to make some kind of weird macro that does it automatically I can
Do this with a regular expression?
So I'm going to go down and I'm going to try to write a regular expression. What am I trying to match?
I'm trying to match any word character any optionally any number of word characters followed by a number?
followed by one or more white space
Not called sorry followed by a comma followed by my double and white space followed by any number of word characters
Look okay, [so] [I] matched these that looks pretty good now. I want to capture
The first name in the last name or the last thing for the first name, so this is the books app. Oh
I lost it
Shoot [goosal] get edited out let me write that again
[comma] some amount of white space okay, so I want to capture
That last name right to any sequence of word characters
And I want to capture that first name so now I've captured those and I want to replace
Right I want to first see the first name
space
the last name so this oh sorry two comma one remember
group [zero] is
Everything so group [two] is the second group right the first name which is last name comma first name
I want to show that and then the other one, wha I'm really. I don't know if I'm making any sense here
Okay, now I'm going to do replace all boom I did it
So this is the kind of thing that you can [really] use regular expressions for in
Kind of [find] and [replace] here's another scenario. I don't know how many of these snares is worth going through but
You know matching links like this is Markdown
Right I'm gonna. I'm going to just do this really quick and do one more
What if I want to match I guess this assuming my syntax is Markdown. What if I want to match any?
Link Written in this remark down syntax and translate it [to] htML. Let's look at how I might do that
Okay, so I want to match a literal square bracket right remember square bracket is a meta character
So I want to match a literal square bracket, and then I'm gonna do something a little bit goofy [ah] I?
forgot about something with quantifiers
Which is now just two if you are still watching this somehow however many hours [into] videos about regular expressions
I missed something important in the previous video. I'm going to do dot
star
Very dangerous to say I'm going to say I want to get a bracket followed by anything
what do I match look at this bracket followed by anything look there's some brackets followed by anything followed by what a
closed bracket
Look at this
Bracket followed by anything followed by a closed bracket now this actually worked, but let me just do something for you here
Look, what happened?
it didn't match Google and test separately because
Dot star is anything like this closed bracket like this space like this parenthesis remember dot star is greedy
[I'm] trying to match Bracket followed by anything up until just keep matching
Until you can no longer match and then there's a closed bracket
So this is what's known as a greedy modifier?
Dot star is by default greedy
It will continue to match if I want to get google and test as separate matches. I need to make that modifier not greedy
Question mark is a way to change a quantifier to make it not greedy
question mark means optional, but it also paired with a quantifier means not greedy, so if I put this question mark [Right] [in] here
Dot star question mark It will no longer be greedy and look what the difference
I've now matched Google and test separately so that's kind of important and just to keep testing this idea out
To make sure it's working [I] am going to put those on separate lines
okay, so now I am going to
Match that followed by a literal parenthesis followed by HTTP which
Followed behind dot star not greedy followed by another parenthesis
So you can see I have now matched any links written in Markdown, so this could be this is Google
You know and this is itp and also right, so I have some text and has links in it
I don't want to convert it to HtML
So now I'm going to replace [it] first. I need to capture
right I need to capture the name and
I need to capture the url, so I'm adding additional parenthesis look how crazy and cryptic. This is getting but I'm cat
I'm capturing the name as group one and the url is group two so now I want to say replace it with a href equals
group two O quote group 2
Followed by group one that close tag, so this is saying take the Markdown Syntax
capture the name
The title and the Url and then replace it with the htML formatting and let's look at this and I'm going to do find replace
Did that work oh?
What's not working here fine, but what did I miss bug here?
No results found okay. I messed something up oh
That should not have a backslash there. Sorry a backslash got typed in there. I'm just going to show this to you again
This is I got a document. These regular expressions somewhere and like a gist or something. This is that regular expression
and now
I'm going to say replace there. We go replace replace and you can see now
I've taken each of those and replace it now if I had a document that with thousands of pages long [I] can instantly do all
Of them, there's one more piece
related to capturing groups that I want to show you beyond just
referencing the group in a finding replace operation
And this is referencing a group in the regular expression itself and that's called a back reference
And I'll show you that in the next video