2.4: Regular Expressions: Capturing Groups – Programming with Text
- December 21, 2023
- Posted by: MainInstructor
- Category: Go Regular Expressions
Video Title: 2.4: Regular Expressions: Capturing Groups – Programming with Text
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
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
good
Appreciate you! This made things much easier
can some one tell me the interface, where Sir is explaining?
Thanks alot!! Was googeling around for at least half an hour, still being confused afterwards. This made me understand capturing groups in 6min. And you have a great enthusiastic vibe!
awesome bhai
You explained it very well. Thanks dude
You are a great teacher. I didnt understand grouping that well. Now I kind of like regular expressions. I am having goosebumps
Dear Coding Train, Please what is the name of the application you used in this video for the regex explanation
Absolute help. Helped me through a new regex project
Thx
good job
Thank you!!!!! I had not understood this topic, but now I do and all thanks to you
I'm 5 years late for this video but the explanation is just WOOOOOW!! Awesome! I'm gonna keep this in my library.
Literally the best regex video I ever found! The instruction on coding platform is explained so badly but this one is amazing! The examples are so easy to be understood.
Did I miss the part why the dash has to be first?
Why did u add “” at the beginning 😮
Merci !
Brother, you are amazing. Thank you.
This is the first time I get here, but I have to say you're damn great! You made this part of regEx clear and simple. Thank you so much, mate. Regards from Spain!
Hi
I am facing issue to achieve below requirement.
Between alphanumeric shouldn't allow to enter more than 3 spaces.
awesome thank you for your wonderful explanation
Great video. Clear elaination. Thanks
Can't thank you enough. This video right here just cleared all my confusion regarding backreference & sub_groups.
how does he do the dotted lines what
love u
You are the best! Thanks!
Can't thank you enough sir…for this piece of gold
Your energy is something else. Good stuff man!
Thank you for the awesome video! I have a quick question: I tried this ^(?d{3}[-|.|)]d{3}[-|.|-]d{4}$ which matches examples like 917-555-1234
646.555.1234
or (212)867-5509. But is there a way that simultaneously, I could modify this code to also match something like 6042627466 (without any string in between)? thank you so much!
Thank you Sir for your Regex series. Well explained
code replace with xxx without removing '.' or () or '-'
((?)(d{3})([-.)])d{3}([-.])d{4}
THIS IS AWESOME!!!! I've been searching and asking for a solution to my problem and this solved it
Really clear, thank you so much! Always had some irrational fear to regex now I'm loving them, I'm gonna use them everywhere lmao!
Exercise at 6:34 would be:
re: ((?)(d{3})([-.)])d{3}([-.])d{4}
replace: $1$2$3XXX$4XXXX
Shouldn't it be [(.*)]((http.*?)) at the end? Or you just don't need the escape anymore after using a group parenthesis?
If Tim and Eric did a coding tutorial series, the ending title would like this.
This man's energy is just incredible, Oh my god I wish every teacher was like him, thank you man
3:54
You are a legend. So helpful
Is group zero ($0) everything because the first thing Regex does is copy the entire text set into memory?
This is quite helpful! Thanks.
videos are super awesome, but i am confused in condionals, if the preceding character is suppose 1, then the next character should be between 0-7 and if the preceding character is 2 then the next characters should be between 0-5.
I mean matching in the phone numbers and region codes
11:51 why does [.* ?](http.*?) change to [(.*?)]((http.*?)) 12:50 without the closed ] while capturing?
thanks