Thanks for coming to InDesignSecrets.com, the world's #1 resource for all things InDesign!

FindBetween: A Useful GREP String

Hey, with the help of Peter Kahrel’s GREP in InDesign CS3 book, I was able to figure out how to do something in InDesign that I’ve always said was possible to do with GREP, but didn’t really know how. Not only is it a handy Find/Change action, but it’s very easy to modify for different situations that designers are often confronted with.

The action is this: Find [some text] that’s in between [whatever], and then apply formatting to just the text, not what’s surrounding it. One example would be formatting parenthetical text without formatting the parentheses themselves: turn (this) into (this) and (that other thing) into (that other thing) all at once, throughout the story or document, with a simple click.

Here’s the GREP expression that finds one or more words of parenthetical content, but doesn’t include the parentheses themselves in the found instances:

(?<=\().*?(?=\))

If you copy and paste that GREP string into the Find What field in the Edit > Find/Change > GREP panel of InDesign CS3, and then click the Find First button, InDesign selects the first instance of parenthetical text, but not the parentheses themselves. Cool!

That means that back in the Find/Change > GREP panel, you can change just the found text’s formatting by specifying what you want in the Change Format area (leave the Change To text field blank). Here I’m specifying that InDesign should italicize parenthetical text, but leave the parentheses untouched:

1-findbetween.gif

Easily Modify the Search
Below is the same GREP string, but this time the characters that govern what the “surrounding items” should be are highlighted in red so you can see what I’m talking about:

(?<=\().*?(?=\))

The first instance is an opening parenthesis, which needs to be “escaped” with a backslash so InDesign knows it’s a literal parenthesis, not some more GREP code. The second instance is the closed parenthesis, again preceded by a backslash to escape it.

I typed these in myself, but it’s simpler to let InDesign drop in the special code that GREP needs. Just choose the special character you want from the dropdown menu next to the GREP Find What field:

1-findbetween2.gif

So, to find text surrounded by a pair of em dashes (but not the em dashes themselves) you’d change both instances of the red characters so it finds an em dash instead of an opening or closing parenthesis. According to the dropdown menu, GREPese for an em dash is a tilde followed by an underscore:

(?<=~_).*?(?=~_)

Here’s the string to find text in between any kind of double quotes, nice and simple:

(?<=).*?(?=)

In case you’re wondering, Peter says this type of GREP search is called a “Lookaround” — a combination of a Lookahead and a Lookbehind. All the “look*” type of GREP searches share one thing in common … they let you tell InDesign to find some text based on a character that precedes or follows it, but not to include that character in the found text itself.

Save the GREP String

If you think you’d find this useful, don’t forget to click the disc icon in the GREP panel of Find/Change so you can save it and recall it from the dropdown menu of saved searches from now on. I called mine “FindBetween.”

Just for fun (and to test out sharing saved searches), I uploaded my FindBetween search … it’s a tiny XML file …. in case you want to download it. Drop it into the GREP folder inside your Preferences > Adobe InDesign > Version 5.0 > Find-Change Queries folder on your hard drive.

Anne-Marie Concepcion

Anne-Marie Concepcion

Anne-Marie “Her Geekness” Concepción is the co-founder (with David Blatner) and CEO of Creative Publishing Network, which produces InDesignSecrets, InDesign Magazine, and other resources for creative professionals. Through her cross-media design studio, Seneca Design & Training, Anne-Marie develops ebooks and trains and consults with companies who want to master the tools and workflows of digital publishing. She has authored over 20 courses on lynda.com on these topics and others. Keep up with Anne-Marie by subscribing to her ezine, HerGeekness Gazette, and contact her by email at amarie@cpn.co or on Twitter @amarie
Anne-Marie Concepcion

Latest posts by Anne-Marie Concepcion (see all)

  • - November 30, -0001
Related Articles
Comments

88 Comments on “FindBetween: A Useful GREP String

  1. I work in the publishing industry and often have to format books, such as cookbooks. The text comes to me with tags at the beginning and end of each block that needs to be styled a certain way. This would be easy if the tag was at the beginning and/or end of each paragraph or line but it is not. I might have an entire ingredients list that begins with a tag, such as:

    then lists the ingredients and after the last one, ends with:

    The same goes for multiple paragraphs of text – codes at beginning and end of multiple paragraphs, not each one.

    I am trying to figure out if it’s possible to format these easily. I tried:

    (?<=).*?(?=)

    but it only recognizes the first line. It seems the end paragraph mark messed things up. Any advice you can offer will be greatly appreciated. Thanks!

    • Lisa, did you figure this out? I am in the same exact situation: A list of ingredients, para breaks at end of each line. First line starts with a hard code, . Last line ends with a close code, . I want to apply a Para style to all paras within those bracketed hard codes. I’ll go search the forum but if anyone has a link to the convo, please let me know!

      • Oops, sorry. The codes are:
        Start: Open angle bracket + I + close angle bracket
        End: Open angle bracket + forwardslash + I + close angle bracket.

  2. @Lisa, as you can see in comments before yours, our site destroys a lot of grep codes, especially things with angle brackets. It thinks it’s html code or something.

    In general, I would recommend asking questions on our forum instead of a particular blog post. (Click forums in the nav bar up above. You’ll need to have a free membership to post.) You’ll still need to “escape” those special character with backslashes, but more people will see it there.

  3. Hello, any help is greatly appreciated.

    I have this code that “applies style to any character between and including the pipes (\x{07c})”

    (\x{07c})(.+)(\x{07c})

    Works fine.

    But, the PROBLEM: I can’t get it to let me specify any character within this: not a letter, not a caret….nothing. Any ideas on how to do this?

    Basically, I have a normal paragraph that includes examples of HL7 messages. Like this. For example, |AB^Abbott^^^MVX|

    WHY: In my full samples of Code (separate Code paragraph style) I have the Carets=red, the Pipes=green, etc…. I’m trying to duplicate this, but I need it to only be between the pipes because these characters also exist in the paragraphs independently not as a coded sample.

  4. Jamie, the problem is to locate any given character somewhere inside a string, you would need a positive lookbehind of undetermined length. InDesign does not support that (that’s not so weird, I have yet to find a program that uses GREP that can).

  5. hi, please help me with these script in indesign CS3

    app.findGrepPreferences = NothingEnum.nothing;
    app.changeGrepPreferences = NothingEnum.nothing;
    app.findChangeGrepOptions.includeFootnotes = true;
    app.findChangeGrepOptions.includeHiddenLayers = true;
    app.findChangeGrepOptions.includeLockedLayersForFind = true;
    app.findChangeGrepOptions.includeLockedStoriesForFind = true;
    app.findChangeGrepOptions.includeMasterPages = true;
    app.findGrepPreferences.findWhat = “(\[*\\d{1,2}|\[~8{3}\])/(\[*\\d{1,2}|\[~8{3}\])/(\[*\\d{4}|\[~8{3}\])”;
    app.changeGrepPreferences.changeTo = “$1@@$2@@$3”;
    app.changeGrepPreferences.underline = true;
    app.changeGrep();
    app.findGrepPreferences = NothingEnum.nothing;
    app.changeGrepPreferences = NothingEnum.nothing;

    I found that it can work with Find/Change Box in Grep but not with the java script, why??

  6. You need to double each and every single backslash in your findWhat expression.

    Any random questions on scripting are better asked in the Scripting forum. You can go there (surprise!) by clicking the word “Forum” at the top of this page. You can register for free (suprise again!) and then ask away.

  7. Anne-Marie’s examples are for single-character things like text within parentheses or dashes, but I’ve found the expression works wonderfully no matter how many characters. For instance, I had some UUID code bracketed by at the rear. The expression (?<=\) found just the UUID. What Anne-Marie said bears repeating: this expression will find anything between anything. What a gem!

  8. Please excuse my previous posting. I didn’t use the proper HTML encodings for markup sensitive characters, but the gist of my comment is unchanged: Anne-Marie?s examples are for single-character things like text within parentheses or dashes, but I?ve found the expression works wonderfully no matter how many characters occur in front or behind. A truly useful expression!

  9. I am trying to make sense of GREP find and replace and does sound like what i’m trying to achieve should be able to be done by all the comments above.
    I am trying to find all instances of:

    ‘Source:’

    where it fills within parentheses.

    I then want to format everything inside the parentheses and including the parentheses to a certain Character style.

    Any help would be much appreciates.

    Regards
    Ross

  10. Hello – -this script finds all the text after a tab and before a return. Works like a dream in my paragraph style, thank you…

    (?<=\t).*?(?=\r)

    What I woudl love to find out is how to tune the script to find all the text between TWO consecutive tabs and a return…

    Would be incredibly useful; trying to assign a character style to numbers in an index…

    thanks for your time and assistance!

  11. I had a problem with setting up a document that imported xml from a CMS tool that would allow variable laser text but I need to also not only find anything between anything but to also include the anythings and colour them magenta so the printer could replace them.

    ie: $Salutation$ $First Name + Last Name$

    I found that using this with a minor tweak would work like this:

    \$(?<=).*?\$

    Just as a note anyone who deals with printer and variable text will know that they normally use but this will not work, but following a call to the printers and a test they found they could use $ signs instead of

  12. Is it possible to use a GREP solution to add a period to the beginning and ending of every string of character styled italics? I’m trying to figure out how to add markers to over 3400 film titles in a film encyclopedia.

    • Hello. Thanks for your website, I’m learning very much with your posts. I have a problem, I’m searching a grep style to apply a character style if the title has less than 14 words or to apply other style if the title has more than 15. Someone told me that I can do this with grep but I can´t find nothing similar. Do you know how can I do it?
      Thank you.

  13. how can i convert al numbers
    before
    4846303
    393251561
    44010
    14560656
    12702185
    44763

    that what i need
    4,846,303
    393,251,561
    44,010
    14,560,656
    12,702,185
    44,763
    how can i do it please

  14. Hello:

    I want to share a issue: I placed a Word text into Indesign. The original text had a lot of superindex numbers but on Indesign looks like: Lorem24. There are any way to using GREP to substitute all at the same time? Perhaps making a code to looking for numbers joined text?

    Thanks in advance!

  15. I am trying to GREP adjust text ${q://QID62/ChoiceTextEntryValue/1}. I want to select everything from $ to } and apply a character style to it (make it orange). Does anyone know how to write the GREP code?

  16. Hi I am looking for an automatic formation of some parts of a text.

    Example:
    Koral, S.M. (2013). Mercury from dental amalgam: exposure and risk assessment. Compendium of Continuing Education in Dentistry (Jamesburg, N.J. : 1995) 34, 138.

    I used ((?<=\)\. ).*?(?=\.)

    But if you have a ? or ! it doesn't work and in addition i want the . ! or? to be italic too.

    I am stucked does anybody has an idea?

  17. This is a great solution to a problem I’ve been having with finding/changing sentences that have *some* italics in them. But it’s part 2 of a two part solution. I’m having trouble with the first part.

    Basically, I want to *tag* italicised words with a character on either side of the word/sentence. So if “this is in italics.” could become “(this is in italics).” You’d think you could just do a GREP find/change, with !$0! in the change box, with “italics” set in the Find Format bar. But all this gets me is “(this)”. For whatever reason, $0 is only grabbing the first four characters. Is this a bug, or how it’s supposed to work?

  18. Hello
    How do I search the brackets with parentheses in InDesign while in the midst of the special characters such as comma (,) available
    Like (amir, 12)

Leave a Reply

Your email address will not be published. Required fields are marked *