5

InDesign GREP rules can get a bit mind-bending. Suppose you want a paragraph style with a GREP style rule that applies a character style to everything except text after a certain character. Something that doesn't apply to characters after a certain character, but applies to everything else?

For example, imagine a long list of article titles. Some are short titles, some are two parts with a short title then a colon then an elaboration. You want the whole of the short title ones to be emphasised, and everything up to and including the colon to be emphasised in the long ones:

  • Adders of Angola
  • Brilliant Badgers: the brawniest beasts of Britain
  • Caravanning Crazes of Coastal Canadian carp
  • Danger: the dastardly ducks of Denmark

I've tried a bunch of rules but none seem to work for this. I thought it might be possible using a 'negative lookbehind', like (?<!:.*). or something, but nothing like that seems to work: it looks as if 'lookbehinds' and 'lookaheads' can only apply to the character immediately before the character you're looking for.

Any GREP experts got any ideas?


Here's one thing I figured out after a lot of browsing and trial and error, which I think logically should work, but doesn't work, for reasons I can't figure out:

\<[^:]*|\<[^:]*:

In theory, that should mean "Grab everything from the paragraph start until the end of the first run of characters that aren't ':', or, if there's a run of characters that aren't ':' from paragraph start until a ':', grab that including the ':'".

In practice, it just applies to everything. Can't figure out why.

6 Answers 6

4

I don't have InDesign to check this, but assuming it behaves the same as the tester here, I believe this should work:

^.*?(:|$)

That's just matching everything from the start of a string up to either the first colon or the end of the string.

3
  • Works perfectly! Didn't occur to me that you could use the 'or' bar within the subexpression brackets like that. I'm trying to understand the role of the question mark: is that to make it choose whichever is shorter of the possible matches? Apr 26, 2013 at 13:31
  • Great! The question mark is exactly that. It makes the * repetition "non greedy" or "lazy" (see here), so it finds the match with the fewest repetitions instead of the most.
    – David Mear
    Apr 26, 2013 at 14:58
  • Surely this would not match the colon itself, though, as specified in the question? Dec 14, 2017 at 8:48
2

Short and simple:

^[^:]+:?

which means "everything from the start that is not a colon, optionally followed by a colon".

"Everything from the start" -- the ^ -- needs to be specified because otherwise the GREP style will be applied from the start of a paragraph up to the first colon, and then again from that colon up to the next colon; and then again and again until the end of the paragraph. Which comes down to "actually the entire paragraph, from start to end".
Each GREP expression is applied as many times as possible inside each paragraph (quite logically; if not, the default expression \d+ would only match the first set of digits in each paragraph).

".. that is not a colon .." -- the [^:]+ matches all characters except the colon itself. If there is no colon, it will match the entire paragraph.

".. optionally followed by a colon" -- the final :? is to match the colon itself when the previous part ended just before it. It is to apply the same formatting to the colon as to the previous text.

0
2

You don't need GREP to do this, you can use Nested Styles.

Under paragraph styles go to Drop Caps and Nested Styles. Under this window there is a button to create a new Nested Style. After adding one go to the drop down and select the character style you want to bold with, then where it says 'Words' replace this with ':'.

This will bold all text in a line up to :, if it doesn't have a " then the whole paragraph should be bold.

1
  • 2
    Hi Joshua, welcome to Graphic Design stack exchange. As it stands this answer is not very descriptive. Could you elaborate on how you would achieve the results using Nested Styles?
    – Ryan
    Feb 24, 2014 at 18:40
1

This is the closest I've managed:

\<[^:]*|[^:]*:

It means:

  • \<[^:]* - from the start of the paragraph (\<), grab things that aren't colons ([^:]), and grab any number of them (*)... (this gets lines that have no colons, and lines with colons up to the first colon)
  • | or...
  • [^:]*: grab any number of things that aren't colons ([^:]*) that are followed by one colon (:) (this extends the selection on lines with a colon to include the colon itself)

It works fine, unless there are two colons, in which case it applies to everything up to the last colon, not everything up to the first colon:

  • Just Annoying: why I'll never be :-D about emoticons

It doesn't stop at the first character as intended. But, it can be used if there's not going to be a second occurrence of the 'stop' character.


Bizarrely, \<[^:]* on its own, or \<[^:]*|:, seem to just grab everything. Can't figure out why.

1

I'd cheat (but I do that a lot).

Make sure every line ends in a colon, even the short titles which have no subtitles.

Use your GREP to make everything up to the colon bold.

Run a second S/R to find everything with "colon-hard return" and make it just "hard return" (which removes colons from any line without a subtitle).

1
  • This wouldn’t really work if you wanted (understandably) to automate it by making it a GREP style. Dec 14, 2017 at 8:49
0

Here's another imperfect answer. If it's just a case of making something look different, you could do the opposite of what the question asks: have everything emphasised, grab everything after the character, and apply a style that de-emphasises it.

This is pretty simple:

:.*

It starts with whatever the first character is then keeps going, so it doesn't have the problem above.

It's not a complete answer as it won't work in cases where it has to be the first part that has the style, but it will work in some cases.

Not the answer you're looking for? Browse other questions tagged or ask your own question.