Skip to main content
Inspiring
August 12, 2020
Answered

GREP First Instance Only

  • August 12, 2020
  • 2 replies
  • 5167 views

I have a large directory I'm trying to format. Each entry has a list of items followed by a descriptive paragraph. Each item in the list has a label followed by a colon. I want to style the label along with the colon, making them bold and all-caps. I had success with the following expression:

.+\:

Which ends up like this:

PHONE NUMBER: 123-456-7890

WEBSITE: www.adobe.com

The problem is, that occasionally one of the descriptive paragraphs will also have a colon, resulting in this:

OVERVIEW: THIS TEXT SHOULD NOT BE STYLED: and should return to normal after only the first colon.

I've been trying various expressions to get it to work and either I get all the text styled, none of the text styled or the above problem. Any help is appreciated! Thank you!

This topic has been closed for replies.
Correct answer Jongware

GREP is greedy by default; it tries to match as much as possible. Make your expression non-greedy by adding a question mark:

 

^.+?:

 

or use a nested style "Up to" and then type a colon.

2 replies

mycc3
Known Participant
April 19, 2021

Similar case. But I want to get the found string without the colon.

If I would use following term it would be the same problem, it will become greedy:


Searching for:

(.+)?:

Replacing with (formatted): 

$1\r

 

That's the result:

Is there any solution to search for "OVERVIEW:", but replacing it with "OVERVIEW[new paragraph]" = not with the colon?

 

THANKS!

mycc

Known Participant
April 19, 2021

Try this:

 

find:(.+?):

 

Jongware
Community Expert
JongwareCommunity ExpertCorrect answer
Community Expert
August 12, 2020

GREP is greedy by default; it tries to match as much as possible. Make your expression non-greedy by adding a question mark:

 

^.+?:

 

or use a nested style "Up to" and then type a colon.

Inspiring
August 12, 2020

Thank you! Worked like a charm; I was putting the ? AFTER the colon, and also escaping the colon..