Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Building subexpressions

From: Learning GREP with InDesign

Video: Building subexpressions

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.

Building subexpressions

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.

Show transcript

This video is part of

Image for Learning GREP with InDesign
Learning GREP with InDesign

42 video lessons · 12836 viewers

Michael Murphy
Author

 
Expand all | Collapse all
  1. 1m 33s
    1. Welcome
      1m 4s
    2. Using the exercise files
      29s
  2. 7m 56s
    1. What is GREP?
      1m 53s
    2. Text searching vs. GREP searching
      2m 35s
    3. Working with GREP and InDesign
      3m 28s
  3. 46m 4s
    1. Using metacharacters, the building blocks of GREP
      6m 37s
    2. Escaping out metacharacters
      2m 49s
    3. Building with wild cards
      9m 9s
    4. Understanding undocumented wild card "opposites"
      3m 11s
    5. Specifying locations
      7m 4s
    6. Learning the undocumented location metacharacters
      4m 45s
    7. Using repeat metacharacters and defining the shortest match
      5m 45s
    8. Specifying exact matches and ranges
      2m 52s
    9. Finding content that doesn't exist with zero functions
      3m 52s
  4. 43m 26s
    1. Creating "or" conditions
      5m 24s
    2. Building subexpressions
      5m 52s
    3. Using character sets to create custom wild cards
      7m 3s
    4. Using negative character sets
      3m 2s
    5. Finding around text with lookbehind and lookahead
      8m 1s
    6. Building with modifiers: Case sensitivity
      4m 0s
    7. Building with modifiers: Single-line and multi-line
      3m 10s
    8. Using InDesign-compatible Posix expressions
      6m 54s
  5. 49m 18s
    1. GREP styles vs. nested styles
      6m 10s
    2. Styling specific words or phrases
      3m 18s
    3. Describing inconsistent text
      6m 59s
    4. Describing and styling prices
      6m 55s
    5. Applying multiple character styles to the same text
      6m 8s
    6. Describing and styling email addresses
      10m 48s
    7. Dynamically fixing orphaned words with GREP
      9m 0s
  6. 33m 30s
    1. Adding more to the mix: GREP Find/Change
      1m 41s
    2. Understanding queries
      8m 20s
    3. Using formatting and styles as Find/Change criteria
      5m 20s
    4. Preserving and recalling using subexpressions
      7m 49s
    5. Backreferences in search queries
      3m 8s
    6. Cleaning up text with GREP
      2m 45s
    7. Creating a GREP-based text cleanup script
      4m 27s
  7. 43m 45s
    1. Describing imported spreadsheet data
      6m 56s
    2. Rearranging imported spreadsheet data
      7m 17s
    3. Applying styles and formatting with GREP
      11m 14s
    4. Describing and standardizing phone numbers
      9m 20s
    5. Inserting anchored objects with GREP
      8m 58s
  8. 27s
    1. Goodbye
      27s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Learning GREP with InDesign.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.