Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Design and personalized recommendations.Start Your Free Trial Now
- View Offline
- Using pre-built find/change expressions
- Reordering names in an exported list from a database
- Working with GREP and fonts
- Establishing character formats with GREP style
- Using escape characters to customize code
- Accessing the fly-out menus to specify search commands
- Applying global conditions using GREP
Skill Level Intermediate
I was once working on a book project that had a lot of historical dates in it like 1492. So I send the manuscript in and when it comes back from the editor all of those dates had commas in them. Like suddenly it became 1,492. I was flabbergasted. What I'm supposed to do with that? I actually had to go through and fix every single one of those dates removing just the comma, and that would have been a nightmare without GREP. Now this is not the document I was working on. But it has the same problem.
All of the years have commas in them, and I'm going to show you how I can use GREP to remove just those commas. To do this I'm going to open the Find/ Change dialog box with a Command+F or Ctrl+F on Windows, and I'm going to type in the GREP tab. The code I'm looking for is a year and years are going to be based on numbers. So I need an any digit. That's \d, any digit code. In this case I know the first digit is followed by a comma, so I'll type a comma, and then I could just do three more of these digits. There is the number digit followed by a comma and three more digits. There is one problem here and that is that this will find any four- digit number that has a comma, and the ones I want only come after dates. And I happen to know that in this document all the dates are something like March 4th and then the year.
So I'm going to go to the beginning of this GREP code and I'm going to put in another digit followed by another comma and then a space, because I know that this will find all the instances where its a date and then the date of the month followed by a comma and then the year. Let's try it out. I'll click Find and it jumps right to the first instance. Find Next and it goes to the next. Pretty good. But now here is the problem. I need to take all of these except the comma and put them into the Change To field. Whenever you want to take text that's set up as a wild card in the Find What field and put it in the Change To field, you have to put it in parenthesis.
So for example, I'll put a parenthesis before this first digit and the first comma and that space, because I want to save all of that, and I'm going to put it down here with $1. Whatever is the first instance of the parenthesis gets put into what's called $1 GREP code. Now I'm going to grab the second digit that I want which is the first digit of the year and I'll put that down here as well, with a $2. The second parenthesis is a $2. Now I don't want that comma there but I'm going to grab it anyway, and then I'll grab that final chunk, the last three numbers in here and I'm going to put that down here with $4.
So I want 1, 2 and 4, but I'm leaving out 3, which is the comma. I notice that in the Change To field the little eye icon is here which draws my eye down to the bottom and I can see it has some formatting applying to it. Always be careful of that little eye when you do not want to apply formatting, in this case I certainly do not. So I'm going to click on the Trash Can icon to make that go away. Now let's go ahead and try it. I click Change All. It went to the whole document and in one click it changed all the years throughout the entire document, 55 of them much faster than it would have been by hand.
Okay this is great. Now let's go ahead and do something a little bit more tricky. Let's reverse the order of these names. I want the last name first. I want this to be Monroe, James. Adams, John Quincy. Can we do that with GREP? Sure no, problem. Let's start over. I'm just going to delete these, and I'm going to type in some codes that will find those names. The first thing I know about these names is that they are at the beginning of the paragraph. So I'll go to the flyout menu here, go to Location pop-up menu and say this is going to be starting at the beginning of the paragraph. Then I need to define what it is that I'm looking for.
I'm looking for something that looks like a name, and I'm going to put it in parenthesis because I know that I'm going to be wanting to use later. So I'll open the parenthesis and then I'm going to find something that's a wild card called the Any Word character. Any Word is like an uppercase or a lowercase character or a number, lot characters fall into the category of letter that might be in a word. That's \w, and I'm going say there is going to be one more of those. I know that this word is always going to be one or more characters. The Space character however never fits into the word. So this will not match that first space character. It will go up to the end of the first word.
Now I have got a problem because in some of these cases I have got James Monroe, which is two words, and in some I have got three words, John Quincy Adams, and in some I have like Ulysses S. Grant. So there is an initial in the middle. I need to be able to search for all of those and handle them properly. I know that I'm going to be needing whatever it is that I'm capturing later, so I'm going to just start with a new open parenthesis here and I'm going to type in a code for any of those things that might appear and it's going to be an either or situation, so I'm going to use a square bracket. It's going to be something inside the square brackets, which means this or that.
I know it might be a space there or maybe there is a individual word character like \w or maybe there is period, and because its inside one of these square brackets I don't have to escape it with a backslash. I'll just leave that as a period. So it could be any of those things coming next. And I know that there may be more than one of them, so I'll put the plus sign there. So this is going to be one or more of either a space or a word or period. Now I'll close that parenthesis because that's going to close the next little chunk that I'm going to using later. But is that chunk always going to be there? Well in the case of a two-word name it's not going to be. All I want is the first and the second piece, there is nothing in between.
So in this case I'm going to put a question mark after that little chunk of code there. So this whole parenthesis might be there or it might not be. What I do know is always there is a space. There is always going to be a space between these two characters. So I just type a space here in the Find What field. And now for the last name. The last name is going to be pretty much the same as the first. Any word space more than one but it always is going to be followed by a comma. So I'll just type a comma in there, and then close my parenthesis.
It looks complex but let me just go over it one more time. Beginning of the paragraph any word with one or more characters followed by a word that may or may not be there which is going to be either a space or a word character or a period character. All of those things are going to be the second name and then when, again, it may or may not be there and then finally a space and another word followed by a comma. And that matches this pattern perfectly. That was the hard part. Once I have got the GREP code, now I just have to put it back in the right order. So, I'm going to say Change this to 3 because that's the third thing in the parenthesis as the last name and I want to start with my last name. So I'm going to say start with $3 then put a space in there between the names and then say do the first one first name and the second name.
No space between 1 and 2 because if there was one it would have been captured by this second element here. But at the end after 2 I do need a comma, because I want to make sure that this is followed by a comma before it goes into the rest of the line. Okay let's try it out. I'm going to search the entire document and with one click I say Change All, it goes through and the all the names are reversed. So sure, using GREP codes to find text and maybe applying formatting to it is pretty cool. But being able to actually move the text around? That is awesome!