is now part of CreativePro.com!

Favorite GREP Expressions You Can Use

157

GREP is an incredibly powerful technology for finding and replacing text. It shows up in several places in InDesign, notably the Find/Change dialog box and the GREP Styles feature. But you don’t always have to figure out all the grep codes yourself! Use the table below to find a grep expression that is close to what you’re looking for. You may need to tweak it a little, or combine some of these to achieve your task.

Then, if you have a grep code you want to share with others, feel free to add it to the comments below. If it’s really helpful, I may steal it and add it to the table in this blog post. Or, if I think of new ones, I’ll just update this table from time to time.

Note: Please don’t use the comments below to ask “what’s the grep code for…”? Instead, use our forums to ask those kinds of questions. Let’s focus the comments below on solutions, rather than questions.

And remember: you don’t need to memorize all these codes. Always look for that @ flyout menu!

Expressing Or

(Milk|Dark) Chocolate Milk Chocolate or Dark Chocolate
(m|M)ilk (c|C)hocolate Milk Chocolate, milk chocolate, milk Chocolate, Milk chocolate
[mM]ilk chocolate milk chocolate or Milk chocolate
[^m]ilk ailk, bilk, etc., but not milk
(?i)milk Case insensitive: Milk, milk, mILk, etc.
colou?r color or colour (the ? means the previous character or expression may or may not be there)

 

Wildcards

[cC][\l\u]+e Any string of letters that starts with c and ends with e, such as chocolate, Chocolate, case, coarse, etc.
\<c\w+e\> Any whole word that begins with c and ends with e
\bc\w+e\b Exactly the same as above, but using \b for ‘word boundary’
\s+$ All spaces at the end of a paragraph
\s+\Z Remove spaces at end of story
[\l\u\d.]+@[\l\u\d.]+ Simple e-mail address (e.g. [email protected] or [email protected])
\d{4} Find any four digits (the curly brace expression is not found in the InDesign @ menu)
[-\u\l\d._]+@[-\u\l\d_]+\.[\u\l]{2,4} More ‘robust’ email (e.g. [email protected])
\d+/\d+ any fraction, such as 1/2, 22/7, or 355/113
(?<!\d)(?<!/)\d+/(?!11)(?!0)(?!9)\d{1,3}(?!/)(?!\d) Robust fraction grep: Will find fractions, but leave out dates such as 6/98, 9/11, or 6/17/2012. Unfortunately, it fails on 355/113 (which happens to be a reasonably good approximation of ?, proving that nothing is perfect).
[[=a=]] Find any kind of a, no matter the accent, including a, á, à, ä, etc.
(?<=\d)(rd|th|st) Find st, rd, or th ordinals (such as 1st, 2nd, 3rd) that follow a digit?in order to apply superscript to just the ordinal (not the digit).
(?<=\().+?(?=\)) Find any text inside parentheses, but not including the parentheses. This uses ‘Positive Lookbehind’ and ‘Positive Lookahead’

 

Escape!

\$\d\.\d\d Find a dollar sign followed by one digit, a period, and two digits. Note that the dollar and dot have to be ‘escaped’ because they normally mean something different.
\(.+\) Find anything inside parentheses (again, parens need to be escaped)
\Q(a+b*c?)\E Escape everything; that is, find (a+b*c) exactly, without having to worry about escaping each special character.

 

Fun Tricks

Description Find Change to
Add an anchored object at the beginning of each paragraph (cut object to clipboard first) ^(.) ~C$1
Reverse first and last name (but watch out for middle names or initials, too) ^(\w+)([ .\w]+)? (\w+) $3, $1$2
After reversing the names (see above), fix any last names that started with Van or De. ^(\w+, [ .\w]+) (Van|De) $2 $1
Find and remove duplicate words \b(\w+)\b \1 $1
Find and remove duplicate paragraphs/lines in a list ^(.+\r)\1+ $1
Find lists in which people have actually typed a number followed by a period followed by a space, and apply automatic numbered list style. ^\d+\. ?(.) $1 (and apply paragraph style that includes auto numbers)

 

To learn more about GREP, visit: www.indesignsecrets.com/grep

David Blatner is the co-founder of the Creative Publishing Network, InDesign Magazine, CreativePro Magazine, and the author or co-author of 15 books, including Real World InDesign. His InDesign videos at LinkedIn Learning (Lynda.com) are among the most watched InDesign training in the world.
You can find more about David at 63p.com

Follow on LinkedIn here
  • Jongware says:

    A word of warning about this one:
    \s+$ All spaces at the end of a paragraph

    \s is for “white space” and that includes paragraph returns — so replacing this with ‘nothing’ will remove all multiple returns!

    If you don’t want this, then it’s usually enough to remove spaces, tabs, and non-breaking spaces:

    [~S\t ]+$

    ’cause these are the most frequent to pop up.

  • Incredible list David! Thank you.

  • Phyllis says:

    Awesome! Thanks!!!!!!!!!

  • James Fritz says:

    Great list. There are probably enough GREP tips out there for a grepsecrets site:)

  • Jesse Kaufman says:

    https://www.regular-expressions.info/ has tons and tons of info on regular expressions (which is what grep uses) … of course, different flavors of regular expressions differ, but it’s usually not too hard to translate from one to another :)

  • Peter Kahrel says:

    David — Very nice list. Some more words of warning, though:

    \$ should find the dollar symbol, but it finds just the first instance in a text — a bug. Search for \x24 instead (24 is the hexadecimal code of the dollar symbol).

    \(.+\) matches everything from the first opening parenthesis to the last closing one. To match, well, matching parenthesis, use \(.+?\)

    Peter

  • Jongware says:

    @Jesse: perhaps this interests you. On a web page of mine I list ID’s GREP capabilities the same way Jan Goyvaerts does.

  • Mike Rankin says:

    Awesome list.

    I’ll use any excuse to make a GREP style. I use the braces David mentions above when I write my InDesign tips for Twitter. I made a simple GREP style that applies a character style to the first 140 characters in a paragraph:

    ^.{140}

    That way I know when I go over Twitter’s character limit and have to shorten the tip.

  • Dave says:

    I LOVE GREP STYLES.

    I have a continuation paragraph style and a grep style searching for any text inside and including quotation marks. So when I type

    See “Grep rules” on page 2A

    it will grab “Grep rules” and make it bold.

    The grep I use to accomplish this is (including quotes)
    “.+?”

  • @Dave that reminds me of the “find between” GREP string I wrote about a couple years ago: https://creativepro.com/findbetween-a-useful-grep-string.php

    @Mike: you write your tweets in InDesign? Um … did you know that Twitter has a field you can type them in? ;D (and a character counter?)

  • Eugene Tyson says:

    Cool list thanks for sharing :)

    I’ve got one for changing times to the format we use, to keep things consistent

    (\d+).(\d+)( |\S)([ap])[\.m]+

    $1.$2$3 $4.m.

    This changes

    5.00pm ? 9.00pm
    2:00 pm ? 3.00p.m.
    9-00 am ? 8.00 pm

    to

    5.00 p.m. ? 9.00 p.m.
    2.00 p.m. ? 3.00 p.m.
    9.00 a.m. ? 8.00 p.m.

  • Mike Rankin says:

    @Anne-Marie Where’s the fun in that?

  • Glen Saville says:

    Cheers David, this is really helpful :D

  • surabasan says:

    Hi All! Is it possible to GREP change a substring to superscript?
    I have 11117, but I need 11117
    (without whitespaces)?

    Thanks in advance

  • surabasan says:

    sorry, “17” must appear as superscript

  • Jongware says:

    Well, David stated

    “Note: Please don?t use the comments below to ask ?what?s the grep code for??? Instead, use our forums to ask those kinds of questions. Let?s focus the comments below on solutions, rather than questions.”

    but a solution could be searching for (?<=111)17 and putting superscript in the Replace field.

  • SmartyGuy says:

    Great list, very helpful. Thank you!

  • JRB says:

    Hi,

    for catching email address I use this one :
    \S+@\S+\.\S{2,4}

    ;-)

  • Chuckie says:

    When ever I have time, I love to curl up to a good GREP. Thanks for being so generous, David.

  • profeivan says:

    My favorite one is this GREP Query that looks for paragraphs which not end with period.

    ([^\.])$

    Change to

    $1.

  • Chris Thompson says:

    One minor point on the any kind of ‘a’ GREP: It doesn’t find the fourth one of these: a à å ?
    which is a Cyrillic a.
    But it does find A uppercase (latin ones that is)

  • Chris Thompson says:

    Hmph, it was a Cyrillic a when I pasted it in, but now it’s a question mark.
    View Source says charset=UTF-8.

  • Jongware says:

    Chris, it’s because the Cyrillic ‘a’ is not a variant of Latin ‘a’. If that was the case, the Greek alpha ‘?’ would also be considered a variant.

    For what it’s worth: Adobe has nothing to do with this particular behavior. The character equivalences are determined by the Unicode Consortium. Checking their definitions for the alpha, for example, shows that these characters are all equivalent and thus can be found with [[==]]:

    alpha tonos
    alpha psili
    alpha dasia
    alpha varia
    alpha vrachy
    alpha macron

    — just to name a few :D

  • Chris Thompson says:

    Thanks Jongware – yes, I was aware it’s an entirely different character, but thought it was worth making it explicit that the description of the GREP expression as “any kind of a” didn’t include the non-Latin ones.
    The Unicode equivalences thing makes a good deal of sense.
    Also vaguely related is the use of such characters to spoof web addresses, which was a security concern when they came in (IDN – internationalized domain names).
    do syidaniya
    (since it won’t accept Cyrillic characters)

  • Chris Thompson says:

    or even do svidaniya.
    or possibly Doh! svidaniya

  • Josh V says:

    Here’s one that will remove (4 letter and less) orphans from the end of paragraphs with punctuation. The “\d?” on the end is in case there is superscript… it doesn’t catch everything but works pretty well.

    Use it with a “No Break” character style with “No Break” checked under Basic Character Formats.

    \\s\w{1,4}[[:punct:]]+\d?\r

  • Josh V says:

    Opps, that’s supposed to be…

    ” \\s\w{1,4}[[:punct:]]+\d?\r “

  • Josh V says:

    Hmm, the first part isn’t showing up…

  • Rob says:

    this doesnt wok for me:

    profeivan
    My favorite one is this GREP Query that looks for paragraphs which not end with period.

    ([^\.])$

    Change to

    $1.

  • Olwen Bruce says:

    WOW!
    I am so glad you took all the time and trouble to learn this wonderful resource and been generous enough to pass it on.

    I have a client who imported text formatted with tabs and returns to make it fit; of course as soon as the font was changed, nothing fitted. Grep has made it possible to fix this in a short amount of time compared with trolling through 100’s of pages looking for returns and tabs.

    Olwen

  • Cem says:

    Can i replace

    1.Question Text

    to

    1.^tQuestionText

    Thanks…

  • @Cem: Well, it depends. If the “1.” is from automatic numbering, then you should be able to just change the automatic numbering definition to use a tab. If it’s real text, then you can search for (\d\.)Q and replace it with $1\tQ

  • Jongware says:

    @Cem: what David said, but you might be better off with this: search for ^(\d+)\.), replace with $1\t

    (It will add a tab after every set of digits and full stop at the start of a line, which is probably what you want.)

  • Cem says:

    Thanks all.

    Jongware, your expression isn’t matched. what is the problem i dont know. did you test it?

    but david’s expression solved problem…

  • Cem says:

    Oops!

    Now it changes;

    1.^tQuestion Text

    to

    1.^t^tQuestion Text

    How can i ignore double TAB?

  • Jongware says:

    Cem, that extra tab wasn’t in your original question…

    Try this one: search for ^(\d+\.) ?(?!\t) and replace with $1.\t — this will explicitly only find digits at the start of a paragraph that are not followed by a tab (that’s the part at the end inside (?!…)).

    I didn’t test this one but hey, I should be able to do this from memory by now.

  • Sivakumar says:

    how to find text with mdash in waild card

  • Cem says:

    I know, i’m asking too question. I’m sorry for this.
    Thank you for answers.

    This answers are helping other users.

    How can i underline the known some words(farkl?d?r, de?ildir, yap?lmaz…) befere question mark…
    But question mark not underlined.

    farkl?d?r? –> farkl?d?r?

  • Hey..
    I need some help with GREP.
    I want to style some text inside parenthesis depending on the last word ie:

    normal body text (+0.33 pr. ap)
    normal body text(+1 pr. ad)

    If the last word is “ap” i want it to have style1 and if it ad style3.

    Thanks for you answers.

  • Jongware says:

    Buster: if those are paragraph styles, you can set them using Find-and-replace. Search for ap\)$, leave the Change-to field blank and put your style1 in the Change To Formatting field.
    If your style1 and 2 are character styles, to how much of the text should they be applied?

  • @ Jognware

    I want the text inside the parenthesis get character style1 or style2 depending on the last word.

    IE:
    normal body text (+0.33 pr. ap) <- +0.33 pr. ap would be green
    normal body text(+1 pr. ad) <- +1 pr. ad would be yellow

  • Jongware says:

    Piece of cake. Try this one:
    (?<=\().+? ap(?=\)$)

    Please don?t use the comments below to ask ?what?s the grep code for??? Instead, use our forums to ask those kinds of questions.

  • @ Jongware

    Thank you so much
    and sorry for posting here, I were just so frustrated over not being able solve it.

  • Andrea Rosellini says:

    is there a way to automatically add a non breakable space just after the first ? of an indented text, and one just before the last one?
    I mean: There’s too many options -(non breakable space) although not all of them are quite easy(non brakable space)- to achieve your goals…

    TIA and sorry for my poor english

  • Keith Cross says:

    Dave’s expression for text within quotes is different than the instructions at this help page: https://help.adobe.com/en_US/indesign/cs/using/WSFB3603CC-8D84-48d8-9F77-F3E0644CB0B6a.html.

    Dave’s trick worked, the instructions did not. This could be a CS4 vs. CS3 issue…I’m not familiar enough with GREP to know, I’ll hit the forums to find out more, but I hope I can figure it out because its not clear where to go from within InDesign to find “all things GREP.”

  • Jongware says:

    Keith, what example in Adobe’s help are you referring to? If it’s Example #1 (Finding text within quoation marks), that’s because their example removes the quotes as well. Dave’s is to just find them.

  • Ganeshlal says:

    Dear all,

    I need a help in Grep. I need to replace all alpha list style which is having a tab in front (a) and a tab & followed to a tab in front “a.” and a tab & followed. Any help will much appreciated.

    Thanks,
    Ganesh

  • Chris says:

    Hello, you people seem very knowledgeable, I have a problem, my language has many words that begin with an apostrophe eg ‘ave and ‘olia
    I cannot get this to spellcheck, I can’t even add it to a spellchecker, but without it I have huge problems because ave and olia are also words with a totally different meaning. All ten of the vowels need this ability, we also use macrons over all vowels sometimes. Spell check does those fine, but I’m really struggling with the initial apostrophe
    Can anyone help me?

  • Michael says:

    Hi All,

    is there a way to search with GREP for any digit which is assigned a specific Character Style and replace it with no text but a specific Paragraph Style? Searching part is not a problem. But what do I put into Change to window? Leaving it empty, retains all digits in the text though assigns correct para style. But I need them gone. Basically a 2 step process – but I was wondering if there is a 1-step of doing that.

    Michael

  • Michael says:

    Ganeshlal,

    Did you try this:

    Search: \t(\()(\w+)(\)\t

    Replace: \t$2.\t

    Michael

  • Michael says:

    Andrea,

    do this:

    create a character style, call it No Break or something similar. Assign it as No Break in the Basic Character Format options.

    Search: – (?=[\u\l])|(?<=[\u\l]) –

    and replace with this character style or add this GREP style into the paragraph style options. This way whenever you have this combination it will be assigned no break automatically.

    Michael

  • Jongware says:

    @Michael: Add (.) at the end of your search string, and put $1 in the Replace field.
    If the Replace field is empty, ID only applies formatting. This way, you force a replacement, if only for the single character you are picking up from behind your digit.

  • Rex theRunt says:

    I have a GREP challenge I can’t solve – any ideas?

    I need to apply a different style to the last bullet in a list (to allow more space after than the regular bullet style).

    I thought I could search for bulleted style followed by a regular paragraph style, but I can only search for one para style at a time.

    Does anyone know how to do this?

    • I found a way to do this, but you don’t need GREP to do it.

      Use a paragraph style for your bulleted lists. Tell the span to span all columns, and then set your after span space to whatever the measurement is that you wanted.

      It will treat all the bullets as if they were one paragraph and it will add after space on the last bullet.

      I know this is way after you asked your question, but I hope that this helps someone else who is searching this thread for the same answer.

  • @Rex: No, there’s no way to do that within InDesign, sadly. Check out the Style Utilities plug-in from in-tools, which can help with that (and many other formatting challenges): https://in-tools.com/products/plugins/style-utilities/

  • Dwr says:

    I wanted to apply small caps to all capital letters in a paragraph, but not to those makred in bold (or another character style).

    Is there a solution via GREP styles?

  • Stacey says:

    ok, I’m new to GREP.. I need a GREP that will change the style of a URL within the body copy of a document. I’ve created a style. But I can only get the first three characters to change (www). Below is what I have, but I don’t know how to get the remaining information to change.

    [www/./]

  • David Blatner says:

    @Stacy: The “how to target a URL” is one of the longest-standing discussions in GREP history, I think. :) Here’s the one that John Gruber came up with at Daring Fireball (click link to see his explanation):

    (?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»????]))

  • Stacey says:

    David, thanks. That’s exactly what I needed. I made one modification:

    (?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|(http|ftp|www)\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()]+|\(([^\s()]+|(\([^\s()]+\)))*\))+(?:\(([^\s()]+|(\([^\s()]+\)))*\)|[^\s`!()\[\]{};:??.,?«»????]))

    (http|ftp|www) – added http and ftp into the string which allows me to search and change all three.

  • Rain says:

    Hi I am trying to come up with a search for variable prices
    Price: $48.00
    Price: $32.00

    The prices themselves are variable
    HELP Please

  • Norman says:

    Hi I am trying to come up with a search for add Index entry for Each Text box in my documents (i have aproximaly 1000 text boxes…)
    Grep find/change don’t add index entry…

    Help Please

  • burak says:

    Hello, I need help in this. I want to delete every letter variation after word BEX. For instance Title-BEX-14 char-style-override-8 should become Title-BEX . How can I do this?

  • @Rain: You can probably look for \$\d+\.\d\d (dollar sign, one or more digits, dot, two digits). No?

    @Norman: You are correct that InDesign doesn’t let you do this. Look at the IndexMatic script at indiscripts.com

    @burak: Perhaps something like BEX.+ (which means BEX followed by every character until the end of the paragraph)

  • Matt Appleton says:

    Hello ? – this great 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!

    Original script found here:

    https://creativepro.com/findbetween-a-useful-grep-string.php/commen t-page-2#comment-550882

  • Leah says:

    @ Eugene, love your GREP, it’s just the sort of thing I’m after but it doesn’t seem to work perfectly. If there is already a space between the number and the am/pm as in “8.00 pm” then it will become a double space. Anyone know a fix?

    Eugene’s post (a few years back) is here: https://creativepro.com/favorite-grep-expressions-you-can-use.php#comment-491565

  • Ignacio says:

    If I want to find something after the beginning of a pragraph, I use symbol “^”, but…

    What if I want to find a character that IS NOT in the beginning of a paragrah?

    Thanks

  • Darquise says:

    I’m trying to find the beginning of a paragraph to end of paragraph with a specific style. Then I want to insert a tab in front of the found text. I need an actual tab character (i can’t just change it in the style). Any assistance would be greatly appreciated.

    Thanks.

  • Darquise says:

    After tons of research I answered my own question by reading up on some of the posts – I should have just done that in the first place.

    Find: \<.+
    Find Format: "my para style"
    Change: \t$0

  • John says:

    I am trying to create an expression which will find digits in text that I can then automatically superscript so as to indicate footnotes. I have come up with this but it feels clumsy to me and there must be something more elegant and straightforward.

    (?<!\s.\.)(?<!\d\.)(?<=[.\.])(\d{1,2})

    So for example in the following text only the 1 and the 2 at the end of the sentences would be superscripted. All the other numbers would stay normal. So far it is working but I am worried that there is an instance that I haven't considered which would mean my expression would have to change.

    With 17 of the G20 countries quietly erecting some form of trade protection in the six months immediately following the global financial crisis, it is not surprising that business has grown more skeptical of the power of the Free Trade Agreement.1
    Only one in 20 Australian companies exports their goods or services.2 This is despite the opportunities provided by three decades of growth in the economies of our global neighbourhood, across the Asian region.
    Australian industry has gone through a major transformation through the same period, with the phased collapse of tariffs and other forms of industry assistance. In the past two decades, Australia has become one of the most open economies in the world with average tariffs at less than 3.9 per cent. (p.34)

  • Jongware says:

    John, this is one of those things you cannot get 100% right. Best you can hope for is not too many false positives (numbers that are *not* to be picked up) or false negatives (numbers that are notes but are not seen).

    First, I simplified your expression to this basic construction:
    (?<=\.)(\d{1,2})
    That is, just 1 or 2 digits right after a full stop. Since it also picks up decimal numbers, make it ignore these with a negative lookbehind:
    (?<=\.)(?<!\d\.)(\d{1,2})
    This still picks up the one page number. Now, make it check for a compulsory space after:
    (?<=\.)(?<!\d\.)(\d{1,2})(?=\s)
    This works for your example text. Only thing is, if your last footnote number is at the very end of a story, there is no 'space after'. But you can also check the inverse: *not* not-a-space-character.
    (?<=\.)(?<!\d\.)(\d{1,2})(?!\S)

    • John says:

      Thanks for that Jongware. I have been using GREP for a few years now, but am only just starting to use it to achieve more complicated results in the long docs that I am working on. It is quite arcane but quite exciting to see what you can achieve after some mental gymnastics. A lot of this sort of work (i.e. avoiding runts, footnoting, italicising publication names, etc.) is done manually in the place where I am employed and I am trying to convince the editor, who does a lot of the work himself, that there is a better, quicker way, but he is still stuck in the 1980s when it comes to page layout. Very frustrating, but at least it keeps me in a job. If I were to succeed I would probably end up retrenched, but hopefully we would just produce more work.

  • Bill Jelen says:

    Thanks for this great article. I had a book where the author had used strange syntax for the figure numbers: Figure 1-(1) through Figure 23-(18) and I wanted to change all of those to Figure 1.1 through Figure 23.18. Here is my first GREP that I wrote with your help:
    Find What: (Figure\s\d+)\-\((\d+)\)
    Change to: $1.$2
    Thanks again!

  • Mr. Excel! (Bill Jelen). That’s great! A “clear parentheses” GREP.

    So nice to see you here. Readers: Bill Jelen is the “David and Anne-Marie” of Excel. Check out https://www.mrexcel.com. I know Bill because I taught him and his assistant some project-specific InDesign stuff a couple years ago, for the books he writes about Excel and sells on his site.

  • sambath says:

    how to find italic worlds by the help of grep. Please don’t use style formatting. i want wildcard option

  • Alessia says:

    How do you create GREP styles for let say this:

    Sweet Pork (Smaller font)
    Baby Back Ribs (larger font)
    $1.99LB (superscript $, no decimal point, cents in small caps and lb in a small font)… I cant figure it out. Driving nuts!

    Please help

  • Mark Coster says:

    This is a great site and one that I come back to time and time again. I am relatively new to regular expressions, but its easy to see that they are incredibly powerful – i’ve started to make use of them in the publication that I produce. I also referenced this site in my recent blog post (https://www.pixooma.co.uk/blog/2013/07/03/get-to-grips-with-grep/)

    I can see that there are several solutions to finding URLs in text, but let me add in my current solution – I’d love to hear what people think. It’s explained on my blog, but the urls in the magazine I work on don’t have ‘www.’ in front of them:

    (\w+|\w+-|\w+/)*(\w+\.[\l\u]+(\.[\l\u]+)*)(/\w+(/w+|-\w+)*)*

    • Hi Mark, I’m always on the lookout for great GREPs. I tried yours, but immediately ran into a problem … it wouldn’t find the filename extension. For example, for acmedesign.com/about.html, it only found “acme design.com/about” and I’d need it to find the rest, too.

      Here are a few that I’ve saved as queries, though I don’t remember where I got them from:

      “GREP for finding URLs”

      (?i)([a-z][\w-]+:(?! )(?:/{1,3}))(www\d{0,3}[.])?([^ <>/]+\>)|(www\d{0,3}[.])([^ <>/]+\>)

      and “Gruber’s Find ANY URL”

      (?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^ ()<>]+|\(([^ ()<>]+|(\([^ ()<>]+\)))*\))+(?:\(([^ ()<>]+|(\([^ ()<>]+\)))*\)|[^ `!()\[\]{};:'”.,<>?«»????]))

      and “Gruber’s simpler find URL”

      (?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^ ()<>]+|\(([^ ()<>]+|(\([^ ()<>]+\)))*\))+(?:\(([^ ()<>]+|(\([^ ()<>]+\)))*\)|[^ `!()\[\]{};:'”.,<>?«»????]))

      (wish I could remember who Gruber was…)

      • Justin Sligh says:

        John Gruber of daringfireball.net?

      • Mark Coster says:

        Hi Anne-Marie, I looked at the GREPs you posted, but I’ve also been working on this since my last post and I’ve found another solution (I think!) — I’d be interested to know what you think of it. The URL search I need to perform is for the magazine I work on. In the magazine web-addresses do not have the www. or https:// etc in them. The following GREP works for me on all iterations of URLs that I have tried, but what do you think? It seems a bit too good to be true given how short it is, so am I missing something fundamental?

        Here’s the Code: ([^\s]+)\.([^\s]+)

    • Mark, I love your ([^\s]+)\.([^\s]+) code for finding URLs. The only problem I see is that it also finds all decimal numbers (such as 15.2).

      Yes, Gruber as in Daringfireball Gruber: https://daringfireball.net/2010/07/improved_regex_for_matching_urls

  • Cynthia says:

    Can you export find/change grep commands to share with other users? I’d like to share some of my more complicated strings with others in my department.

  • Felicia says:

    Hello,
    I am new to GREP styles and I have a question. I work for an academic press and often have books with hundreds of notes. The problem we are having is when a note number appears right after a closing quotation mark, often they are too close together. I was hoping I could use GREP find/change to find all of the instances of a closing quotation mark is directly followed by a number and insert a hair space in between them. I have been able to find the instances using ~}(\d) but I can’t figure out what should be in the change to box.

    If someone can help me that would be great!

    • Cynthia says:

      Felicia,

      Have you tried ~}~|(\d)

      I clicked on the @ symbol and went down to “white space”. Hair space is listed under there.

      • Felicia says:

        Thanks Cynthia! That worked except it changed the note number, do you or anyone else know how to make a change without changing any of the surrounding characters? That is the part that keeps tripping me up. I can’t figure out how to write a code that will insert a space without replacing the note number.

    • Justin Sligh says:

      Felicia,

      You can create sub-expressions by wrapping them in parentheses. Try the following:

      Find:
      (~})(\d)

      Replace:
      $1~|$2

      $1 stands for the first sub expression and $2 stands for the second.

      Basically we are saying: Find all end quotes( sub-expressions 1) that are immediately followed by a digit (sub-expression 2). Then replace with the results from sub-expression 1, a hair space (~|), and sub-expression 2.

      • Felicia says:

        Justin,

        That worked, thank you so much! That will save our designers a lot of time and headaches.

    • Justin Sligh says:

      Felicia,

      Now that I think about it, I would recommend doing this via a Grep Style. That would be non-destructive.

      Add the following Grep Style to your paragraph style:

      ~}(?=\d)

      The associated character style can control the space between the end quotes and digits through the tracking setting. Something like +50 should suffice.

      (?=) is a positive lookahead. So we are saying: Find an end quote only when it is immediately followed by a digit.

  • John Tierney says:

    I am trying to create an expression so that the first letter of each bullet point is always a capital letter. I am importing copy from different writers and some will treat bullets in the same manner as text following a colon or a semi-colon (i.e. beginning with a lower case letter). Our style guide calls for a capital for which I have created a character stylesheet. The expression I thought would be quite simple ^\l (the first lower case letter at the beginning of a paragraph). The problem I am having is that when I use a soft return the first letter of the following line becomes a capital. Clearly ^ doesn’t distinguish between the end of paragraphs and forced returns. I also would like to exclude “http” and “www” from the taking the Capitals character style sheet where they appear at the beginning of a bulleted paragraph.

    • Justin Sligh says:

      John,

      In the future you may want to post questions in the forum. It will likely get more coverage.

      The following should work:

      (?<!\n)^\l

      (?<!) is a negative look behind. We are basically saying: Find any lower case letter that is at the beginning of a paragraph which is not following a forced break.

  • Bradford Stevens says:

    Hi, I am SO close to figuring out this string, but I just can’t get it right:

    ((?<=\d )(a|A)\.?(m|M)\.?)

    I want to find any variation of A.M. that follow a time EXCEPT "a.m."

    Because I will then change all the incorrect am (or pm, in a separate command) variations to "a.m." But I can't seem to get the "exclude a.m." portion right.

    Any advice?

  • Paula says:

    I am laying out a textbook…some of the words are very long, and using the justify line spacing can leave huge gaps between words…the line and paragraph composer are little help, using one causes problems in the other.

    Is there a way to use GREP to find large spaces and replace them with a ‘third space’…which seems to work well with the leading and all that.

    It takes such a long time to hunt down large gaps and paste smaller ones.

    Thank you, Paula

  • How can I change a series of period to a Tab. Not just one period but a series. Like : my number ……. 444-4444
    merci

  • hesham says:

    Hi

    But I need to use that in the GREP paragraph style, and not in the find and replace box.

    Actually I have a typed text with paragraphs start with a digit followed by a period and a space.
    I already created my paragraph style with Bullets and Numbering but I need to auto delete the numbers inserted manually at the begging of each paragraph. i.e.
    1.space
    2.space
    3.space
    etc.

    Basically I need to search for:
    ^\d+\. ?
    and replace with nothing.

    Many thanks for your help!

  • Martin says:

    Hi,

    Can you please help me. I need to do a search in which I find a Line with a specific number of characters (fx. 28 characters). Is This posible?

    • Justin says:

      Martin,

      Could you elaborate on your need to find these characters? We would need a bit more information to provide a viable solution.

  • Martin says:

    I am making a phonebook in InDesign. And I need do do a search in which I find, all the hits in which the line goes into 2 lines (because of too many characters). I Hope you understand what I mean :O)
    – so I need to do a search in which I find a Line with a specific number of characters (fx. 28 characters). Is This posible?

    • Jongware says:

      Martin, that would be this:

      .{28}

      — any character, 28 in a row. Or, if you want to match the entire line (so you can do something with it)

      .{28,}

      — 28 or MORE. It will match everything up to the end of a paragraph. Note that GREP is not aware of individual *lines*, it can only work with entire paragraphs.

  • Martin says:

    @ Jongware: Thanks a lot – good job – it works fine for me now.
    Now I have a new task: I need to insert a automatic break character before a Format called: Adress – how do I do this?

  • lil says:

    i need clear whole style overrides in indesign by an order

  • IASTATE Agron Comm says:

    You can set up a pattern to find multiple lines of text across line breaks (line breaks, not wrapped lines of text) and place the cursor after your found string using this:
    (?:^.*\n.*\n\K).*

    This finds the third line of text (i.e. the text after the second line break mark in a paragraph) and highlights it.

    It requires typing a paragraph with line breaks (soft returns, i.e. shift+enter), but for something highly predictable like a calendar entry (which is how I am using it) you can take an entire line of text and apply a format to it, regardless of it if wraps visually. In my case:

    Time & location [soft return] (style 1)
    Event title [soft return] (style 1)
    Presentation Title (style 2)
    Blah
    Blah
    Blah

    The key things that I had to draw from various sources were (?:) which basically means “see if this exists, but don’t remember it” and the \K which means “put the cursor here”.

    It took me days of searching and furthering my knowledge of REGEX to figure this out, but thought I would share it in case it helps someone else.

  • Law says:

    Hello!

    I am trying to figure out something that I am not sure is a GREP problem or another issue. Basically I need to find a series of numbers that look like this: XXXX-XXX and add brackets in the beginning and end of them all. End result should look like this: [XXXX-XXX]

    Is there any easy way to do this within the FInd/Replace?

    Thanks!

    • Cynthia says:

      I would try a grep search that uses wildcards.

      Click on the @ symbol next to the Find what: box. Toggle down until you get to wildcard and then over to “any digit”. Do that 4 times, then add a hyphen, then 3 more times.

      Click on the Change to: box and add a bracket. Then click on the @ symbol next to Change to: box. Toggle down to “Found” and then over to “Found text”. Then add a second bracket.

      Your “Find What:” field should look like this: \d\d\d\d-\d\d\d

      And then Change to: field should look like this: [$0]

      Hope that works for you!

  • Hugues denis says:

    Hello,

    I am working with a list of customer information that have the name, the address, the email address and the url. Each information is in a paragraph.

    I would like to use a grep that can find the paragraph containing the name of the customer (the name of the customer is after the paragraph containing the url) to be able to change its paragraph style.

    Can someone help me with it?

  • Hugues says:

    Hello,
    I am working with a list of customer information that have the name, the address, the email address and the url. Each information is in a different paragraph.
    I would like to use a grep that can find all paragraphs containing the name of the customer (the name of the customer is after the paragraph containing the url) to change the paragraph style.
    Can someone help me with it?

  • Campbell says:

    Hi.

    What I would like to do is find all words that are duplicated with in a INDD document and replace with a bolded version of the word, or highlighted. Basically I want this function to make any duplicates obvious so I can go through and double check. This is because I proof a lot of artwork. This would make my job much easier.

    Any help would be much appreciated.

  • Joe says:

    I require a GREP code i can’t figure out, I would like to search a document for any string of numbers, including decimal places, and replace the decimal point with a comma or replace a thousandth separator with a decimal point or a 10000’s separator with a space? so 4.1 becomes 4,1 and 4000 becomes 4.000 and 40,000 would become 40 000 for example.

  • Hughes, Cambell, Joe, and others: Please see my comment in the original article above… it’s far better to ask these kinds of “how can I?” or “what is the code for” questions in our forums, rather than on this blog post.

  • Today, while I was at work, my sister stole my apple ipad and tested to see if it
    can survive a twenty five foot drop, just so
    she can be a youtube sensation. My apple ipad is now broken and she has 83 views.
    I know this is totally off topic but I had to
    share it with someone!

  • Cynthia says:

    Might as well post the link so you can at least get more views. ;o/

  • Ben says:

    Hello, I was in the InDesign Forum, and someone there suggested I look at using GREP for my dilema.

    I have a textbox the I want to make into either a solid, or replace with a bullet based upon the condition that if the textbox has a “1” in it (it would only be either “1” or “0”. In essence I need to turn on or off a box that way. My data is coming froma csv file. I can see the 1 or 0 in the textbox, but have no understanding of scripting to make that on/off thing happen. Do you have any suggestions, or GREP that might work?

    Thanks,

    Ben

    • Ben: I would suggest just doing a find/change… find everywhere that it just has a “1” by itself in a text frame and then replace it with a character from zapf dingbats or some symbol font that looks like the box you want to use. etc.

      • Ben Jarnutowski says:

        Dave, thanks for the suggestion. I need to print these documents, so that is why I need to automate. Not sure is find/replace would work when going to print.

        Ben

  • mapaz says:

    Hello!
    I want to do something in specific. I try to put into an anchored objet all the Titles of chapter, but i don’t know how!!!
    First i put an anchored objet in the beginning of pharagraph whit an expression like this:

    Description Find Change to
    Add an anchored object at the beginning of each paragraph (cut object to clipboard first) ^(.) ~C$1

    Now I want this Find text go inside my anchored object.
    Please help me!
    Thanks

  • Dave says:

    HI, thanks for the info above. All really useful. I have one recurring issue… I have text (for an index listing) that is always supplied in MSWord, with varying amounts of individual dots between the words and page numbers. Despite many attempts to explain about tabs to the client, they always send the text this way and I end up having to manually double-click the dots and add a righthand tab to force the page number to the righthand side of the column.

    Is there something in grep that will search for these multiples dots and replace them with a righthand tab? Any pointers would be great. Thanks Guys… keep up the good work!

    • Dave: You could open the GREP tab of the Find/Change dialog box and search for \.[. ]+ then replace it with ~y
      In the grep code, that’s both a dot and a space inside the brackets — that means, a dot followed by a string of either dots or spaces, so it will find ……. and . . . . . .

      • David Cosh says:

        David, you just saved me two hours work! Thank you so very much. Really appreciated! It works a treat.

  • Ax says:

    Regarding the first expression under “Escape!” (\$\d\.\d\d)…
    Wouldn’t it be better to just use “\$\d+\.\d+”? That way it would find all possible amounts.

  • David, I am trying to write a GREP that will look for paragraphs that have NO COMMA in them, and apply a style to them. Is there a way that you know of to write GREP to look for the absence of a character?

  • Ax says:

    Gavin Anderson, this should work: “^[^,]+$” (without quotes)

    • Ax, thanks! That was exactly what I was looking for, and it works perfectly. Can you explain to me how the negation works, so I can apply it in other code. I understand that ^ refers to the beginning of the paragraph, and the +$ is looking for repeated instances up till the end of the paragraph. But I don’t understand exactly the framework of [^,].

  • Thanks Ax. I’ll give that a try.

  • Volker says:

    I have a question about combining multiple GREPs.
    In my special case i would like to replace every “Normal Space” between a value and it’s unit ( g, ml, kg, L, a.so.) with a “Thin Space”.
    What i have so far is a replacement for every ” g”:

    Search:
    \b˜sg\b|\b g\b

    Replace
    ˜<g

    But actually i have to adapt this expression to every possible type of a unit (g, kg, l …) and run the replacement again. A bit unhandy. How can i combine this into one single replacement-expression?

    Best regards
    Volker

    • Alternately, if you want to stick with find & replace, I believe you could write it like this:

      Find:
      (\d+)˜s(g|ml|kg|L)\b

      Replace:
      $1˜<$2

  • I would suggest that you use GREP in styles for this, rather than a find and replace.

    Your expression in your Paragraph Style would look for a number, space, measurement (Ex. 9 ml) and by use of look behind and look ahead, you would narrow it down to apply a Character Style to just the space between the number and measurement. Set your Character Style to something like 50% horizontal scale, thus automatically thinning all spaces in your document that fall between a number and its measurement.

  • Luiza says:

    I need an expression to select the strings between parenthesis (included) at the end of a paragraph.

    Example:
    Paragraph starts here (I dont want to select this one) because is has some text after it (but I want to select this) (and this) (and as many I have at the end of a paragraph).

    I’m a begginer ih this theme, Could you help me, pleeeeease? :D

  • Justin says:

    Luiza,

    You can find the first instance of something but not the last. So, you can’t do it with one grep expression. However two will do the trick.

    Grep Style 1
    (?<=\().+?(?=\))
    This one looks for one or many characters between and open and close parentheses. It will find and style all within your paragraph.

    Grep Style 2
    (?<=\()[\s\l\u]+(?=\))
    The second will go back over and style only the first instance.

    So basically the character style for Grep Style 2 should match the rest of the paragraph body.

    • Luiza says:

      Thank you very much! I’ll try it!
      I’m sorry to annoy you, but would this Style2 also work if I had something like:

      blabla (dont select) blabla (dont select) blabla (select)(select)(select)

      Or would it select only the first (dont select)?

      • Justin says:

        Luiza,

        Sorry wasn’t thinking. It should be:

        Grep Style 1
        (?<=\().+?(?=\))
        This one looks for one or many characters between and open and close parentheses. It will find and style all within your paragraph.

        Grep Style 2
        ^.+?\)
        The second will go back over and style only the first instance.
        So basically the character style for Grep Style 2 should match the rest of the paragraph body.

        In regard to your comment, style 1 will style all instances and style 2 will style only the 1st instance.

        If you are looking for something more specific, we would have to know more about the content that resides within these.

    • Holly Blake says:

      Hi Justin,

      “You can find the first instance of something but not the last.”

      Do you know how I can find the first instance of a registered trademark symbol?

      Thanks,

      Holly

  • Luiza says:

    Hi Justin, thank you again. I used this last solution, but it still doesn’t fit all my needs.

    Before asking you, I was using this: \(.+\)$
    But it was selecting everything between the first “(” and the last “)”

    A real paragraph would be this (sorry it’s in portuguese), where I wanted to style only “(Redação..)” and “(Revogado)” but not those two “(dois)”

    “Considera-se caracterizada a atividade preponderante referida neste artigo quando mais de 50% (cinqüenta por cento) da receita operacional da pessoa jurídica adquirente, nos 2 (dois) anos anteriores e nos 2 (dois) anos subseqüentes à aquisição, decorrer de transações mencionadas neste artigo. (Redação dada pelo Ato Complementar nº 35, de 1967) (Revogado).”

    • Luiza says:

      and neither the (cinquenta por cento)

    • Justin says:

      Luiza,

      At this point start to put bandaids on the issue.

      You could add a third style, similar to style 2, which looks for “dois” or other numbers.

      \(dois|três\)
      additional numbers separated by a pipe.

      Someone else may have a better idea; however, Grep is not a comprehensive solution. Consider a character style with a keyboard shortcut as an alternate.

      • Luiza says:

        Ok, thank you. You helped a lot anyway.
        I guess I cannot automate 100% of the job :)

      • Jim says:

        Justin/Luiza,

        Could this do the trick?

        1st grep style:
        (?<=\().+?(?=\))

        2nd grep style:
        (?<=\().+?(?=\) \w)

        The first one changes all text between parens to what Luiza wants the last parens text to be.
        The second one fixes the earlier parens text back to normal because it only changes text between parens where the end parens is followed by a word character. Therefore, any that are followed by another parens or punctuation will not be effected.

        This worked in my testing based on the sample text Luiza provided. Considering it's in a different language, I don't know if there is anything I can't foresee that would prevent this from working.

  • Adep says:

    How do I swap two numbers using GREP styles?

    Year at the beginning of the paragraph and a rating at the end. I have been using Grep find and replace – (\d{4})(.*)(\d\d\r\Z) to $3$2$1 but I need to use styles and then use NEXT styles to style all the text.

    cheers
    Adep

  • Hello,
    my file is something like:
    shiva
    cool
    april
    inverter
    cool
    hot
    cool
    nmo
    cool
    Now i want to grep data between april and first coming cool after april including these words. i want a general solution so that it can apply to all files.
    Kindly help.
    Thanks in advance

  • ydesign5 says:

    Need to Italic the words between the codes:

    @8:Aggression, the Crime of Crimes

    Words change coding does not. It’s for an index. How do I set up my find/change?

    • ydesign5 says:

      Deleting code when I post. Basically Ihave code on both side of the copy. Telling me that it should be italic. I want to do a find/change

      • ydesign5: I’m not sure exactly what you’re trying to do, but this image should show you how you can make some text in-between two html “em” tags italic:

  • Ali Cinki says:

    I am working on an Islamic calendar. We put 6 praying hours for 20 cities every day as a list. Now i must change the order of the cities and i can use only 9 dollar signs for replacing. Is there a way using more then 9 dollar signs?

    • Ali: I don’t know how you can do more than 9, sorry. But I wonder if there is a better way, using excel or something. That is going to be tricky.

  • Andy says:

    Hello

    How would I get GREP to find first capitalised word (and those following) up to a beginning parenthesis e.g.

    Mathematical methods, models and modelling (MST210)

    So it would select “Mathematical methods, models and modelling” but only that?

    Many thanks

    • Justin says:

      Is it always at the beginning of the paragraph?

    • If it’s in the middle of a paragraph, how can you tell the difference between the capital letter at the beginning of a normal sentence (like at the beginning of the paragraph) and the one that starts the class title?

  • Andy says:

    Here’s an example:

    You’ll begin with one of two modules: Object-oriented Java programming (V000) or Designing applications with Visual Basic (V000). Next you’ll learn how to analyse requirements, plan, design, implement and test a range of web applications in Web technologies (V000).

    • Justin says:

      Cross-references or hyperlinks might be one solution. Doesn’t automate the process but you would be adding interactive features and the ability to adjust the style of these references at a later date.

    • Andy: I would put this in the “almost impossible” category because there just isn’t any difference between a normal sentence and the class name.

      But okay, here’s an attempt. If this doesn’t work, start up a query in the forums instead (those are better for back and forth, rather than comments at the end of a blog article).
      \u[^.,:]+?\(\u+\d+\)

      That will find an uppercase letter followed by any number of characters that are not a period, comma, or colon, then followed by parentheses containing only uppercase letters followed by numbers.

      But this won’t work for your first example because the course name has a comma in it! You could remove the comma from the “not” statement, but then won’t find other ones properly.

  • Andy says:

    Thanks for all your help guys

    Will try your suggestions and report back tomorrow.

    Thanks again

  • Mike Rankin says:

    To reduce spam, comments have been closed for this post. If you want to start a related discussion, head over to the Forums and create a new topic.

  • >