Skip to main content
Inspiring
May 19, 2022
Answered

Help with a GREP to find only first match in a paragraph.

  • May 19, 2022
  • 3 replies
  • 1457 views

Hi

 

I need a GREP to fix a space problem in a book, I want to change the first ( - ) in the paragraph to ( -) and the second to (- ), so I should do it in two steps but the grep applies the first step to the all the ( - ).

The GREP I tried:

( - )?

 

This topic has been closed for replies.
Correct answer Peter Kahrel

So you want to change English/Dutch spaced dashes to French/Spanish/etc. 'semi-spaced' dashes. Assuming that all your space dashes are those that you're after, you could try these two expressions.

 

The opening dashes (I used \x20 for the space character so that you can see it)

 

Find what: \x20-\x20(?=.+?\x20-\x20)
Change to: \x20-

 

Then do the closing dashes:

 

Find what: \x20-[^\x20].+\K\x20-\x20
Change to: -\x20

P

3 replies

Community Expert
May 19, 2022

@Manan Joshi -- I think the parentheses in the example are just to show the spaces before the dashes. But anyway, unfortunately, doing a replacement like you suggest makes a bash of any formatting.

P.

Community Expert
May 19, 2022

Hi @Peter Kahrel, thanks for sharing your thoughts. Apart from the parentheses, I did a quick test before sharing the regex and found that changes in properties like stroke color, or font were properly preserved when the change was made using my grep search and replace. However, I do understand your point.

-Manan

-Manan
Community Expert
May 19, 2022

Well. . . Before and after:

 

Peter KahrelCommunity ExpertCorrect answer
Community Expert
May 19, 2022

So you want to change English/Dutch spaced dashes to French/Spanish/etc. 'semi-spaced' dashes. Assuming that all your space dashes are those that you're after, you could try these two expressions.

 

The opening dashes (I used \x20 for the space character so that you can see it)

 

Find what: \x20-\x20(?=.+?\x20-\x20)
Change to: \x20-

 

Then do the closing dashes:

 

Find what: \x20-[^\x20].+\K\x20-\x20
Change to: -\x20

P

Community Expert
May 19, 2022

Try the following

Find What
(\( - \))(.*?)(\( - \))
Change to
( -)$2(- )

-Manan 

-Manan
AK09MAuthor
Inspiring
May 19, 2022

Thank you, Manan.

That did it after adjusting it to not include parentheses.

 

so it looks like this:

Find:

( - )(.*?)( - )

 

change to:

-$2-

Community Expert
May 20, 2022

Hi @AK09M,

Look at the first post that @Peter Kahrel made. That should be the way to go. My method could cause issues as shown by another post by Peter. You can chain the two search replace operations into one by using the FindChangeList script shipped with InDesign and that way just a single click should do all the edits for you.

-Manan

-Manan