Video Coming Soon...
08: Capturing
Capturing is easily the most useful thing about regular expressions. It lets you "capture" a part of the regex so you can extract it. This allows you to simply pull only that part out, or to rewrite things in new ways while maintaining other parts.
Capture Example
Imagine you have a CSV file with lines like this:
age,weight,height
10,100,45
25,220,85
Let's say you want to extract only the weight part of each line, but ignore the rest. You do that by putting a () (parenthesis, or parens) around what you want like this:
.*,([0-9]+),.*
This will match everything up to the first , (comma), then any number of numbers, then , followed by the rest of the line. The ([0-9]+) tells sed to "capture" whatever matches inside those parens for later use.
Register for Learn Regex the Hard Way
Register to gain access to additional videos which demonstrate each exercise. Videos are priced to cover the cost of hosting.