Skip to main content
Inspiring
July 23, 2021
Answered

small GREP help appreciated

  • July 23, 2021
  • 4 replies
  • 955 views

with the strings:

 

[1234 abcd]

[abcd 1234]

[abcd 1234 efg]

 

I want to match both square brackets and only digits inside, hence

 

[1234 abcd]

[abcd 1234]

[abcd 1234 efg]

 

with some info of how does the pattern works. Thanks in advance

This topic has been closed for replies.
Correct answer Eugene Tyson

You will only need this portion of the GREP

[^[]*?\K\d(?=[^[]*\])

 

And I have been proven wrong - you can find them with a single GREP

Kudos to the OP.

 

If this is correct for you let me know and we can mark it as a correct answer.

Thanks

4 replies

Brad @ Roaring Mouse
Community Expert
Community Expert
July 23, 2021

When I can't get what I want done in one pass, I like to use placeholders... where I temporarily replace something with an odd character combination that Ican easily search for in the second pass.

For this, I would do a first pass that wraps the items I want to change with some placeholder characters.

Pass 1:

Search for :  (\[)([\l\u]*\s*)(\d+)(\s*[\l\u]*)(\])   (same as SJ's, just with brackets to separate the found text)

Replace with : •$1°$2•$3°$4•$5°    (where I put a bullet to the left and a degree to the right of the brackets and numbers.)

Pass 2:

Search for :  (•)(\[|\d+|\])(°)

Replace with :  $2  (applying a style to the found items).

 

We're probably overthinking this as we still don't know what the OP wants to do.. they might just want to extract those items?? 🙂

Inspiring
July 26, 2021

Guys, thanks for all tips, I have to have a while to get through all of them. Original intention was to highliht (distinguish) in any way the numbers in square brackets.

Eugene TysonCommunity ExpertCorrect answer
Community Expert
July 26, 2021

You will only need this portion of the GREP

[^[]*?\K\d(?=[^[]*\])

 

And I have been proven wrong - you can find them with a single GREP

Kudos to the OP.

 

If this is correct for you let me know and we can mark it as a correct answer.

Thanks

Community Expert
July 23, 2021

So I was right it can't be done by a single GREP.

And the OP still hasn't told us what they want to do.

 

FRIdNGE
July 24, 2021

… So you were wrong! 1 Grep code [just for fun!]:

 

(?x) \[(?=[^[]*\d) | [^[]*?\K\d(?=[^[]*\]) | (?<!\D)[^[]*\K\]

 

Good headache!

 

(^/)

Brad @ Roaring Mouse
Community Expert
Community Expert
July 24, 2021

The (?x) was a new one on me. Had to look that one up! But this works!

Legend
July 23, 2021

This can be done with a pair of GREP styles. This works with your examples, and will also work with different quantities of letters and numbers. The first style applies the Bold to the brackets and everything between them. As I have written it, it will not affect bracket sets that do not contain any numbers between them. The second one goes back and overrides the first one by applying a Character Style that matches the Paragraph style text to every letter in the Paragraph. It's a bit less than elegant, in that it restyles every letter in the paragraph, but it gets the job done.

Apply Style: Bold

\[[\l\u]*\s*\d+\s*[\l\u]*\]

Apply Style: regular

[\l\u]

 

Peter Spier
Community Expert
Community Expert
July 23, 2021

I think maybe better to use the None character style for the second GREP expression...

Legend
July 23, 2021

That was my first inclination, but it didn't work - the letters retained the first style. It seemed like the only way to make it change was to apply a new character style.

 

Community Expert
July 23, 2021

I don't think it's possible to have a selection that is broken up - the string has to be contiguous.

 

If you can tell us what you're trying to do - we might have a better way to make this work for you.

Are you trying to embolden the square brackets and the numbers?