Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The text editor I'll be using is TextMate for Macintosh. If you're on Windows, the E Text Editor is a very similar product. So the file that I am going to be demonstrating with is in the exercise files, and its called us_presidents.csv. CSV stands for comma separated values, and it's a list of the U.S. Presidents, with each of the values separated by commas. And those values are the number that they served, their term, their name, the start date, the end date, their party, their state that they came from, and the URL for their Wikipedia entry.
What we want to do for this exercise is to take their name, like George Washington, and separate those into two separate fields, and flip them around. So instead it will be Washington, George. Essentially, instead of just name, we are looking for last name, and then comma, first name; that's what we are going for. So we want to use regular expressions and backreferences with Find and Replace to make that happen. The way that we do Find in TextMate is under the Edit menu; there is Find. If you pull down to that, pull out here to just simple Find. You'll also see that there is a shortcut, which is Command+F, and also before we select that, I just want you to see that down here there is another shortcut Command+G, which is Find Next. I am going to be making use of that as well.
So we pick Find, and it comes up with a basic Find window, and this will let us find just literal text inside the document. If you want a bigger window to work with, you can actually click this here. I am going to go ahead and leave it small. There is no reason that I need all that extra room. Notice here that it says Regular expression. If I don't want to search for literal text, I check this box, and now in this Find box, I enter regular expression syntax. So if this were a problem in the real world that I was trying to solve, I would try and solve it in three steps. I would first write a regular expression that matches the thing that I want to replace. Second, I'd put in the capturing groups to capture the things I want, and third, I would write the replacement string.
So let's start by first writing that regular expression. In order to make sure that I'm getting that first name and last name column, there are a couple of different ways that we could do that. The way that I'm going to do it is I am going to go ahead and just start at the beginning of the line, and then I'm going to go ahead and provide a regular expression that extends beyond the name as well. It's probably not necessary to write quite as much as I'm going to, but it's not a bad set of precautions to take to make sure that you're finding exactly what you want, and nothing else. So at the beginning of the line, I am going to say I am looking for a digit, and it could either be one digit or two digits, followed by a comma, and then the name of the person.
Notice that these first names can include their middle initial and a period, so I am going to say that would be a word character, or a space, or a period inside a set. There's got to be at least one of them, and then it would be repeated. Let's make it not greedy, and then a space, and then we need to do the last name. And the last name, then, would be the same thing, but no period necessary this time. We'll make it not greedy as well. It's a little bit overkill, but I would go ahead and say backslash d, and four to make sure that I was getting those four digits, and that way I can make sure that I am matching exactly what I want; that I am getting the expected column.
So let's try that out. Hit Return, and it found George Washington. Now I am going to use Command+G, just to go to the next one. Scroll through the list, and make sure that it's matching what we expect it to match. So once I've done enough of those to satisfy myself -- you could go through the whole list if you wanted -- click back up here at the top of the document again, let's go back to Find, and now I am going to put in the capturing groups. Now, here is an important point: when we do a match here -- just watch the match again, and see what matched -- anything that's part of that match is going to be part of the replacement.
So anything we want to stay there, we've got to put back there again. So if I want that number one there, I have got to put the number one, but the number one varies from line to line. It's number two when we get to John Adams, and number three when we get to Thomas Jefferson. So therefore, what I need to do is I need to capture it by putting those parentheses around it, and then now I can replace it with itself down here. We talked about backslash one being the way to make backreferences. Some editors, including TextMate, will use dollar sign one when you're down here in the replacement string.
It uses backslash one when we are putting it in the same string; dollar sign one when it's down below. I could capture the comma too, but I am going to go ahead and put the comma in. Let's go ahead and do this name, so we've got the first name, and any middle initial, followed by a space, and then the last name, and then a comma, and then again we want to make sure that we capture those last four digits as well. Now to start with, I am just going to put in dollar sign, two, space, dollar sign, three, comma, dollar sign, four.
That would replace it with exactly what it found; it would not do any transformation. Instead, though, what we want to do is change it so that we have three, comma, dollar sign, two. So now three, which is the thing being captured as the last name, will come first, followed by a comma, and then the first name and middle initial, followed by another comma. Let's click back here at the beginning, and then once we are in that top line, let's click next to find it, and then let's watch it. Let's click Replace & Find.
Do this replacement, and find the next one at the same time. George Washington did exactly what we expected. Let's try John Adams; same thing. So we can go down these, and we can just watch it. I typically would watch to make sure something like John Q worked out. Van Buren; that worked out okay, and then once we are satisfied that enough of these are working, you can just hit Replace All, and it will do the whole list for you. Now we've accomplished our Find and Replace. We've saved ourselves a whole lot of data entry, and a whole lot of typing, by using the power of regular expressions, and the power of backreferences.
So to summarize the steps that we followed, first we created a regular expression that matched our target data; we tested it and revised it using anchors and more specific regexes to narrow the scope. Then we added in the capturing groups. We put the parentheses around the parts we wanted to capture, and you specifically want to capture anything that varies from row to row, because we are going to use the backreferences to reference the actual data that's there as the expression moves down, row to row. And then, last of all, write the replacement string.
We want to make sure we use all captures, assuming that we do want to keep all of them, and add back anything that was not captured, but still needed, such as the commas that we put in between our different values. And remember that you may need to use dollar sign, one, instead of backslash, one. If you followed these simple steps, it will help guide you as you unlock the power of using backreferences with Find and Replace.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 100273 Viewers
56 Video lessons · 113240 Viewers
71 Video lessons · 82124 Viewers
131 Video lessons · 39411 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.