InDesign CS3 introduced the ability to perform GREP Find/Changes, making it easier for us to apply character styles to text pattern strings such as ‘all text’ between parentheses. InDesign CS4 continued the “GREP-theme’ through its ability to lock this pattern based application of character styles into your Paragraph Styles.
The following tutorial is part of a 4-part series on GREP Styles.
Part 2: Change the appearance of numbers (figures) within a given paragraph style
Part 3: Converting text typed in Capital Letters to Small Caps (OpenType feature)
Part 4: Formatting price tags containing dollars and cents with multiple character styles
In this first tutorial we start with a simple GREP style tutorial that looks at how we can automatically apply a character style to all text between parentheses.
Task at hand
All of the paragraphs for which the text between parenthesis must be set in Italic have already had a Paragraph Style “Body Text” applied to it.
Define a Character Style
In preparation for the GREP style, I prefer to create the Character Styles required prior to defining the GREP style components (Character Style and Expression).
To define a Character Style: Highlight some text in the Body Text paragraphs and change its formatting. I’ve changed the Font Style to Medium Italic.
Next choose New Character Style from the Character Styles panel menu and Name the Style. I’m naming mine “Medium Italic”.
Click OK to add the style to the Control Panel. I’m purposely not clicking “Apply Style to Selection’, as the text I formatted with Medium Italic is only used as the basis for the Character Style definition.
Because we set some of the text in Medium Italic (Font Style), our Paragraph Style now indicates with the ‘+’ icon that this format is not native to the Paragraph Style. Let’s remove this format override, by clicing the Clear Overrides icon at the bottom of the Paragraph Styles panel.
It is now time to add the GREP Style to our Paragraph Style.
GREP Style
Right-click the Paragraph Style and select Edit “Body Text” to edit the style. The Paragraph Style Options dialog appears. Click GREP Styles.
First of all lets set the Character Style that we’re going to apply to the GREP Expression we’re inserting in just a moment. From the Apply Style drop down choose the Character Style created earlier.
The Character Style we want to apply is “Medium Italic”.
Oops did you forget to create the Character Style? Don’t worry, from InDesign CS4, you have the ability to create your Character Style right here in the Paragraph Style Options dialog. Have a look at the bottom of the Apply Style pop-up and notice the “New Character Style”.
GREP Expression
It’s time to get GREPPING… so what was that pattern again we were looking for? An Open Parenthesis character, followed by any type of text (alpha-numerical, spaces etc. all allowed), finished off with a Closing Parenthesis character.
Symbols
If you are already a GREP wiz you might be able to enter the required GREP expression in the To Text field. However, I personally prefer constructing the expression bit-by-bit. The Special Characters For Search pop-up is a great help in constructing the expression. InDesign will automatically insert the correct expression data.
Our Open Parenthesis Character is part of the Symbols group.
As you select this component, you might notice that the text entered in the To Text field is now set to \(. Certain symbols are reserved by GREP for particular expression components. The Parenthesis form part of this group of characters. For our GREP Expression to recognise the “(” as a literal character it must be preceded by a “\” character. This “\” is referred to as an Escape Character.
Each of the following characters is reserved by GREP:
* ^ } ] ) $ . { [ ( | + ? ~
If you want to use these characters as a literal character in a GREP expression, you must escape them with the “\”.
Wildcards
Next we’re after our next part of the expression: The any text or words etc. that are inserted prior to the closing parenthesis.
When you are looking for ANYTHING… Think of the fact that you know that this ANYTHING might be an “a” or a “b” or really any character of the alphabet, and even numbers or spaces.
Wildcards are a way of defining an “ANYTHING”. And as we’re looking for any type of character, not just Digits (numbers), Letters (alphabetical) or White Space (spaces), we’re choosing Wildcards > Any Character.
A “.” is added to the GREP expression.
Repeat
You might think at this stage that you’re there… Not quite… GREP Expressions need to know home many times on ef these Any Characters can occur… So we need to know tell the Expression how many times this Any Character can repeat itself.
Let’s assume we don’t encounter “()” (without text between the parentheses). So we’re looking at One or More occurrences.
Shortest Match
So what would happen if there is more than one set of “()” within a paragraph? Well, this is where the Shortest Match option comes in handy. Shortest Match looks for the first occurrence of the “(” and the first occurrence of the “)”, the shortest possible string it can find in the paragraph, and that is exactly what we’re after 🙂
So let’s choose Repeat > One or More Times (Shortest Match). This adds “+?” to the expression.
We’re almost there at this stage. All we need to add to our expression now is the Closing Parenthesis character. We jump back to Symbols for that one.
Choose Symbols > Close Parenthesis Character
Our finished expression is \(.+?\)
Any text between brackets is now set using our GREP style. Click OK to apply the GREP style to the Paragraph Style and see the finished result.
Video Tutorial
Cari,
Thanks for this tip.
Do you think It is possible that grep catches those words coming from already italized, and instead of applying them a character style obtain that grep identifies this tag in the text and apply7 automatically the ch. style to this text? Same for bold, of course.
Hi Cari!
Thank you for sharing your knowledge! I am just getting started in trying to use GREPs and I am struggling with how to code a search and replace for a specific character set within a large catalog document.
In the document, there are hundreds of corporate listings for companies in our association and the way that our database is exporting the info, the phone, email, website and fax info are all preceded by “p -“, “e -“, “w -” and “f -“. I want to change the character style of the letter and dash to Franklin Gothic Heavy from the Book style it is currently set at, but I am having trouble understanding how to incorporate the space between the character and dash into the GREP string. I know that in hex, the space should be should be \x{20}, but what do I need to do for the style change for the single character preceding the space and the dash after the space?
Thank you for any help you can give a beginner!
hi Brian,
You’d need to apply separate grep styles for that. The first one will catch the “p “, “e ” etc. when it is preceding a hyphen, the second one will catch the hyphen when it is following the “p ” etc. What you are using here would be a positive lookhead and a positive lookbehind.
\<[pewf]\s(?=-)
this is a positive look behind that find the pefw characters and space if they precede the hyphen.
(?<=\<[pewf]\s)-\B
this looks for the hyphen when it follows pewf characters and a space.
Cari
Hi Cari
Thanks for the repply.
Well… we have a semiautomated system where text gets imported, and by use of nested styles, formatted.
Now we want to have all text formatted as ‘xxx’-caracter style – which was applied as a nested style – extracted by a script, and compared to a database readout.
An example:
A trade catalog. Item text (1), price (2) and delivery costs (3) are imported, and formatted by use of nested styles.
When layout is all done, and just before sending to the printer, we want to extract all prices (caracter style 2) and compare them to the list of current prices from sales dept.
If I can’t search on the caracter style 2, it gets harder to automate.
The use I have is even more complex (and it is not price we search on), but it is pretty similar to this.
But when I apply a paragraph style with nested (caracter) styles on a text, the caracter style is not assigned to the text. Can it be assigned automaticly somehow?
@Seest If you are looking for particular text through a script wouldn’t you be looking for the same string as you have applied in the grep or nested style through scripting find/change? and isolate that based on the paragraph style in which you’ve applied it? You can use GREP as part of scripting. If you want to physically apply character styles to text using GREP use Find/Change : GREP and set the character style in the Change Format settings. Downside is that this is NOT dynamic like a nested or GREP style.
Hi Cari
I am already working with lightly formatted GREP, but when I use GREP or Nested style (InDesign 6) it only formats the text, but doesn’t apply the character style. Thereby I can’t recognize the character style applied, and nor search for it in a script.
Is that a global issue, or have I done something wrong?
@kisuke, Yes, you can do that.
Find what: -{2,}
Change to: leave this empty.
Click on More Options…
Under Change Format, set the Attributes to Change: In the Basic Character Formats, select the ‘No Break’ option.
hi ms cari,
just wanna ask if i can do change specific characters like “–” double/triple hyphens to apply ‘no break’ character using grep find/replace in indesign…
if possible, what can be the code like?
thanks in advance ^^
Hello, could you please help me GREPing the below:
«word word»
these («») are greek brackets, but i know that it can work with “” also.
but i cannot write the code correctly…
thanks in advance
ms
@swen I think you need to start looking at the ‘end’ of your string. If the numbers are always at the end of a string that starts with http:// you can use l positive lookahead I think. you’d have to give it a try do something like http:// at the START of a word, followed by any non-space character, then digits at the END of the word. Have a look at my Lookahead and Lookbehind blog-posts they should help you with that. 🙂 Cari
Hi Cari,
I really like the tutorial but I’m already looking for a what seems to me a fairly simple problem. Is it possible to use GREP to add a hyperlink to a certain paragraph style. Allow me to explain.
I have put all the article nrs in a document in a certain style (to be able to index them easily), but now I can use these article numbers to combine them with a hyperlink string http://productportal.acertys.com/?webpartid=ACE_ProductDetails.ax&productid= –> here comes the article nr.
That way I’m able to make an interactive technical sheet.
I was even looking at scripting this, but even then it seems not to have been covered by anyone (yet)…
@Cari Yes, I will definitely try to do custom anchoring of the unanchored figures & repeat numbering. If this works, well & good or else as you suggested maybe I will have to explore the scripting idea, though I am a novice at scripting & have only handled design jobs till now
@Mayoor that is true indeed. Forgot about that for a second. Can you not make all of the figures anchored? E.g. using ‘Custom’ anchor settings?
Another thought crossed my mind, but probably a little late as it sounds like you’re already well into layout of this job.
Could you use static captions generated from metadata on the figures? E.g. in Bridge set the figure numbers + caption in a metadata field, then in InDesign add the static caption, and apply the figure paragraph style to it that sets the Chapter number in front of it. Might be worth looking at doing this later on? But still has issues as well, naturally when there are changes in numbering.
Nice puzzle this one.
I think ultimately what you might need is a Script that adds the numbering for you 🙂 based on page position, e.g. ignoring that the parent frame of an inline graphic might appear 10 pages earlier.
You can also think of using a script that at final stages of the project, unanchors all figures + captions, retaining positioning/wrap settings resulting in correct figure numbering.
@Cari I tried out your suggestion by hard coding Chapter number & using automatic lists for the figure numbers, but the same problem persists. Indesign first numbers all the figures in threaded frames from page 1 to 95, then goes to the figure in unthreaded frame on page 2, 10 etc. thus totally messing up the numbering sequence. Later, I found that ID gives numbers sequentially to content in frames in the order in which they are created, not the order in which they appear on page (as explained by David Blatner in Real World Indesign). So, is manual find-and replace the only option for me or does a grep code exist to increase the digit numbers from that point onwards to the end of the document
@Cari Thanks a lot for your quick response. I will definitely try out your suggestion & will get back with the results. In the meantime, was just curious to know if there is a grep code to increase the digit numbers from that point onwards to the end of the document
@Mayoor True combining anchored and non-anchored captions could mess things up a bit. As a workaround you could opt to hard code the ‘chapter number’, and just number the figures automatically. Create figure paragraph styles for each chapter. To be 100% that you don’t get continuous numbering across chapters, use a unique list for each chapter.
I actually tried using the numbered list feature, but it fails because a few of the images are anchored in the text whereas others are not. So, what happens is that the numbering goes hay-wire. Is there any other workaround?
@Mayoor An alternate work method: If each chapter is set-up in a separate document, you can use Chapter-Numbering for the chapter numbers and then set-up a separate numbered list style for the figure numbers only.
@Mayoor
You won’t need to use a GREP style for this. Instead, use a paragraph style for ‘Figures’ and use numbered list feature.
It might be a little tricky to get the second level numbering right if you are using other numbering for sub-heading levels. But a trick I apply in that case is use ‘Level 1’ heading for the first number, and then use a Level heading that is HIGHER than any of the other levels for your headings etc. to get the second number renumbering after a Level 1 heading. Hope I’m making sense?
Cari
Can u please help me with this problem. I am formatting a book with IDCS4 on Win XP platform. There are hundreds of images in each chapter. They are labelled as Fig 1.1, Fig 1.2 etc. With every new edition, new images are to be inserted in between the existing images, with the result that the numbering of the images has to be corrected manually. This is a tedious & error-prone job. I was just wondering if there could be Grep Code or a script which can automate the process of increasing the count by 1 (or 2 as is the case) of the remaining images till the end of the story, whenever an image is inserted in between.
@Garry you don’t need to use a lookahead/behind for that at all… just use a shortest match, and remember that the
[
must be escaped. Here is the answer:\[\d+?]
If you appreciate the help provided, please feel free to make a donation 🙂
Thanks for the info, the [1] … [20] … etc. needs to be superscript when this is in a document it’s has to be changed.
Tanks for your help
Garry
Hello Cari it works with this Grep code.
The only problem I have is how to work with one or two digits.
(\[)\d(\])
(\[)\d\d(\])
Thanks Garry
hi Garry, not sure what you are trying to do with this code (it is easier for me if you provide a text sample of what you are trying to do), but to capture one or more digits use: \d+ instead of \d or \d\d — Cari
Thank you Cari,
I’m a new user to GREP and the only thing I have to do is see how it works, with this pdf I can thry to make it.
And again thanks for your help.
Garry
hi Garry,
Have you read through the recent blog posts I posted on GREP, I provide a very extensive explanation of Lookaheads and Lookbehinds? Also I have made available some GREP Notes as well online from a recent presentation (the links to the blog posts are in that as well): http://bit.ly/Z8JoSl (2.4Mb PDF), I’m thinking if you read through that you’ll learn more about GREP in InDesign.
There is an example in the PDF that explains how to capture something between parentheses, you’d just need to replace them with your square brackets, the opening bracket needs to have a ‘\’ in front of it, because it needs to be ‘escaped’ (the PDF explains all of this).
See if that helps, and if not, post the GREP that you have tried to use, so I can see where things go wrong.
Cari.
Hi Cari,
Do you have a example, Now I have to make a look behind and a look ahead en make it all superschript, and then for 1 or more digits.
[1]
I made some codes but none works I do something wrong but I don’t know what I’m a beginning user of GREP and when you see the code you first have to understand how it works.
Thanks
Garry
A other question I managed to make
(?<=\b(M|m))2|3\b
But another problem how to get
[1] [2]… [10] etc in superschript I can use (any digit)
I know that the character is also used in the GREP code
Thanks
Garry
You could do that by adding a second GREP style to your paragraph style for that Garry. And then apply the superscript character style to it. Cheers, Cari
Thanks for all your help
Garry
😉
Great news Garry. The code that I loaded had an accidental space in it, for some reason, sorry about that! 🙂 Glad it worked 🙂
Hello Cari, It’s working
(?<=\b(C|K))v\b
Hello Gari,
Thank you very much for your quick response I shall try it out.
Many thanks
Garry
Dear Cari,
I’m trying to make a GREP Style in Indesign that changes the width and height for the v behind the Cv and Kv. I made a Body style and a character style when I use \B[v] this will also use the other v characters in other words and it must only apear in this two combinations.
I hope you have an answer for this problem for me?
Thanks Garry
hi Garry,
Just had a quick look at this. \B does the opposite of word boundary (captical letter vs lowercase). However I tried to put the word boundary outside the postiive lookbehind, and discovered it doesn’t pick things up correctly unless you include the boundary marker inside the lookbehind:
(?<=\b(C|K))v\b
(C|K) basically means find a C or a K in front of the lowercase letter ‘v’. (is a quicker notation, then creating two separate GREP styles)
Try the above, it should work — at least it does in CS6 from what I can tell –.
For more information on Positive Lookaheads/Lookbehinds, see my recent series of blog posts on the topic as well. http://carijansen.com/blog/
Cari
hi Simantika,
Aaaah, you’re looking at a Find/Change – GREP in that case.
One of the things you can do with Find/Change is break up your pattern into little chunks called sub-expressions. These are basically brackets around parts of your expression.
In your case that would be the opening bracket, then the text in between and the closing bracket.
The advantage of using a sub-expression is that you can remove parts of what you find, or swap things around as well. E.g. if you find a LastName, then FirstName with sub-expressions, you can set your ‘Change to” setting to put the Firstname first and LastName last 🙂 Each sub-expression in the Change to dialog is listed with a $ and a number. $1 being the first part, $2 being the second part and so on.
Find what:
(\()(.+?)(\))
(that’s three sub-expressions)Change to:
$2
(that’s only putting what was found in the second one back)Change format: apply bold either by setting character style or just the font style, which ever you prefer.
Hope this makes sense 🙂
Cari
Hi,
But i need to delete the brackets as well….. I mean, change the text inside brackets.. say to bold style …. and then delete the surrounding brackets.
Hier is a example on which i am working:
I have text, “blue jumping fox jumped over (3) the tree”
Now from the above text, I want to change “(3)” this part of text
3 should be bold and opening and closing round brackets must be deleted.
So the resulted text will be: “blue jumping fox jumped over 3 the tree”
This should happen only with decimal numbers in the round brackets. If alphabets are inside brackets, they should remain unaffected.
@Simantika, thank you. Yes, you can exclude the opening and closing brackets. You’d need to use a combined positive lookbehind and lookahead in that case.
Try using this as the GREP expression:
(?< =\().+?(?=\))
And you should find that only the text in brackets is affected by the GREP style.
Cari
Hi Cari,
Thats a wonderful info. Is it possible to remove that round opening and closing brackets? sometimes i dont want the brackets but need the inside text with other style.
Those notes are really very helpful, I learned a lot! Thanks!
Thanks for the tutorial, Cari!
Is it possible to set up a GREP style that uses some characters as a guide to a pattern, but that does not affect those characters?
For instance, in the example of the tutorial, would it be possible to apply the character style to the text inside the parenthesis, but not to the parenthesis characters?
hi Leandro, yes that is possible, you use a combination of a Postive Lookbehind and Lookahead, with the parenthesis in Look-codes and the .+ (any character) in between. if you hop onto http://www.indesignusergroup.com and type ‘grip on grep’, select the ‘speaker notes’ I wrote for one of the meetings, it has some good examples in it for you. Hope they help.
Cari Jensen-great tut on one thing Grep Styles can do. I have worked a bit in the Help Files about Grep but for some reason this tutorial seems to be more understandable to me. Thanks so much and I am going on to the other 3 at this time.
Happy New Year!
Steve
Hi Rob, hmmmm… don’t think you can do that with a ‘GREP’ style. I’d be inclined to use a script for that as scripts can count ‘occurrences’. GREP styles don’t count occurrences… but merely look for a pattern. Shortest Match basically looks for the shortest occurrence of a pattern. For instance if a paragraph contains two words in brackets… and you are looking for a word in brackets, it will end the ‘pattern’ at the first closing bracket rather than the second. And it would then find the second word in brackets as well. Hope this helps.
Hello! I have a client whose brand name must be in all-caps the FIRST time it appears, then upper/lower thereafter. I’ve set-up an all-caps character style and – in my paragraph style – have set-up the following GREP (using ‘Brand’ instead of the real name here)…
Brand+?
…I THOUGHT adding the Repeat > One or More Times (Shortest Match) would do the trick, but no luck…it’s capping ALL mentions of the name within the paragraph…Any advice?
@Jane
Please note there is a significant difference between how InDesign and Quark handle Character Styles.
Character styles when created are best designed by first applying the paragraph style over the top of which they will be applied… then define the new style. This means only those attributes that differ from the rest of the text will be included in the style.
I may have just figured out the above, just nested style…
Hi! Ever since I switched from Quark, I have been trying to do what Quark does easily: When I make a paragraph, e.g., “Tx,” I want to create a character style (e.g., “Tx-char”) for that paragraph to use all the time (not to have just have “none” as its char. style). Is it possible to use GREP to make the whole paragraph use the Tx-char style? btw: I’m using a trial version of Indesign CS 5.5
@Camilo
What is it you are trying to do, find the letter ‘a’ when it is between quotes-marks?
you would just use the literal character ‘a’ in that case. E.g. (?<=")a(?=")
What if i want to replace the “any character” character, for a hole word. For example, I want to replace the period “.” for the letter “a” and thats how im doing that, but it just doesnt work.
(?<=“)\a(?=”)
Thank you, Cari, for being the only resource that mentions shortest match. I’ve been trying to figure out how to find HTML-like tags in a doc, strip them out, and format them when there was more than one instance in a paragraph. Shortest match…who knew one little question mark could mean so much? Thank you, thank you!
@Erica Gamet: you are most welcome! Glad I could help.
BTW: Michael Murphy has shown in his great new book on InDesign Styles that Adobe’s list of GREP Metacharacters is incomplete. What about a quick change of
http://help.adobe.com/en_US/InDesign/6.0/WSa285fff53dea4f8617383751001ea8cb3f-6f59a.html
@Jochen:
Yep 🙂
http://carijansen.com/2009/05/03/styles-styles-and-more-styles/
I reckon it’s a must have book for anyone who is serious about learning to work with InDesign productively and using ‘best practice approach’.
Hi Jochen. Great site! Thanks.
Hi Carl,
very good, thanks. Think it is very important to communicate these new technologies proudly and intensively.
Tried something similar just two days ago: with the simple example of figures typogracilly organized in groups of three. Don’t mind the German; GREPs are international 😉
http://blog.smi.ch/2009/07/03/automatische-3er-gruppe-zahlen/
Jochen