#100DaysOfCode in Python Transcripts
Chapter: Days 28-30: Regular Expressions
Lecture: String capturing parenthesis
0:00 A common task is to capture strings using a regex.
0:05 Here we have two strings, 100 and 200.
0:08 What if we want to extract number days of code
0:13 out of these strings.
0:16 Here's how you would do it.
0:18 First, I do a research.
0:25 And I use the capturing parentheses.
0:28 What this will do, any regex inside these parentheses
0:32 that matches the string will be stored in a match object,
0:36 which we can access with groups.
0:39 Hashtag, one or more digits, days of code.
0:46 As it is searched, re is happy to just match the substrings.
0:50 So I don't need make sure that the whole string matches.
0:53 If this would be match.
0:57 Let's do it actually.
0:59 I would have to account for anything that comes before,
1:03 and anything that comes after.
1:06 Of course I need to give it a string.
1:09 And let's see what happens.
1:11 So first of all we have a match object,
1:14 and to get the actual matching string I can do groups.
1:19 And it gives me a tuple of the matches.
1:24 So to get the actual string, I can just use indexing.
1:28 And I got 100 days of code.
1:31 Now this will work the same for 200.
1:36 Let me show search, that was my initial intent.
1:40 Search, then I don't have to account for end time,
1:43 so I those wild cards out.
1:46 I'm going to use 200 to show the match object first.
1:56 And again, 200 days of code.
2:01 So you see the power of regular expressions,
2:02 this is still very simple.
2:05 I can just say one or more digits, followed by a string,
2:08 and it will match both 100 and 200 days of code.
2:12 So that's how you capture strings with the re module.