Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In a previous movie, we saw how the Or metacharacter considers everything before it as one choice, and everything after it as another. But if you want to limit the Or condition or a repeat metacharacter to a smaller portion of an expression, you need to break that expression up into subexpressions. In this example, an Or condition exists that looks for Sir Kay, Sir Galahad or Sir Launcelot. The problem with that is it's not the most efficient way to do this. The Sir is repetitive.
Sir only occurs once and then the Or should really just apply to the names themselves, that follow it. It takes up a lot more room in this very small working space, in the To Text field, so it becomes a problem if the expression gets longer. So one way to write this more efficiently, and the only way to rein in the Or condition to a certain part of an expression, is to use a subexpression. What I am going to do is put my cursor here, after Sir and space, select everything that follows, and delete it.
From the Special characters menu, I am going to go down to the Match submenu and choose Marking subexpression. That inserts an opening parenthesis and a closing parenthesis. Anything that exists within those parentheses is considered the subexpression. The parentheses isolates that as a separate unit within the overall expression. It's also the reason why you can't just define an actual opening or closing parenthesis character by typing them in a GREP expression. You have to put a backslash before them to escape them out.
I'll move my cursor in between those parentheses characters and I am going to type in those knight names. Kay, then the Or metacharacter, which is the vertical slash, Galahad, or, Launcelot and let's see what happens when I click off the page. The effect is the same. Nothing has changed from the previous and longer expression. I've isolated the Or metacharacter to only what exists within the subexpression. So when it looks to its left, here, it only goes as far as that opening parenthesis and considers just Kay, not Sir Kay, one of the choices for the Or condition.
Then it looks after it for just Galahad and after the next Or metacharacter for just Launcelot. So this is a much more efficient way of writing this expression and it's the only way to limit the scope of the Or metacharacter. There are many, many uses for subexpressions and we're going to encounter them throughout this course. They're very critical part of GREP and they're going to creep up over and over as we work. Another instance where subexpressions are quite useful is to allow the repeat metacharacters, like One or More Times or Zero or One Times, to apply to a range of characters or a specific sequence of characters.
For instance, I am going to click off here for a moment and move myself down the page. In the lower half of this page, you can see that Sir Kay appears up here, normally, and then later down in the page, in a couple of places, it's Sir Kay apostrophe S, in the possessive form. If I want to describe either appearance of the name with or without the possessive, I can use a subexpression to do it as efficiently as possible. I am going to go back into the Body Text options by right-clicking on Body Text, choosing Edit Body Text and going into the Paragraph Style Options.
I'll move this over just a little bit so we can see a little bit more on the page and I am going to go to the GREP Styles work area and I am going to clear this expression out. In fact, I am just going to clear out everything after Kay and the beginning of that subexpression. I'll click off here and you can see only Sir Kay is highlighted here, here and here, but without the apostrophe s. Now, one way to handle this is to put the apostrophe in just by typing it and then going to the Special characters menu, choosing Repeat>Zero or One Time.
That puts in the question mark metacharacter for Zero or One Time, which means it may be there, it may not be there, either is a match. Then I'll type in a lowercase s and again put in another question mark, because the s may or may not be there. I'll click off here, and you can see that in this situation it highlights both Sir Kay by itself and Sir Kay apostrophe s, two variations of the name. For something this short, with only two characters, this is probably fine.
It's only a handful of metacharacters, but in a more complex pattern, you can do this a little bit more efficiently. For instance, I could delete the question mark after the apostrophe and instead I'll put my cursor before the apostrophe, type an opening parenthesis, then after the s, type a closing parenthesis. I've now define a subexpression that consists of the sequence apostrophe s as one thing, which means I only need to put one Zero or One Time metacharacter at the end of it, which is what exists here.
Under normal circumstances, Zero or One Time applies only to the one character that precedes it, but when you use subexpressions that Zero or One Time metacharacter applies to the entire subexpression. So I can click off here and nothing changes on the page because this accomplishes the exact same thing, but in a slightly more compact manner, especially if it was more than just an apostrophe s and there were a number of characters in there, a whole portion of a word, for example, that you wanted to make optional. So, that's yet another use for subexpressions.
subexpressions are necessary whenever a pattern, even a simple one like this, reaches a point that requires you to limit a repeat character or other condition to a small part of the overall expression. In more complex expressions, breaking a pattern into subexpressions is absolutely essential.
Get unlimited access to all courses for just $25/month.Become a member
120 Video lessons · 63803 Viewers
119 Video lessons · 71349 Viewers
125 Video lessons · 32152 Viewers
84 Video lessons · 18727 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.