Copy link to clipboard
Copied
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
… So you were wrong! 1 Grep code [just for fun!]:
(?x) \[(?=[^[]*\d) | [^[]*?\K\d(?=[^[]*\]) | (?<!\D)[^[]*\K\]
Good headache!
(^/)
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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]
Copy link to clipboard
Copied
I think maybe better to use the None character style for the second GREP expression...
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Hmm! …
I do prefer 3 Grep styles treating each part: the open bracket "[", then the numbers, and finally the close bracket ]", taking in account the context.
\[(?=[^[]*\d+[^[]*\])
\[[^[]*?\K\d+(?=[^[]*\])
\[[^[]*\d+[^[]*\K\]
These Grep codes seem to be complex but they're not! 😉
(^/) The Jedi
Copy link to clipboard
Copied
I just tried in both CS6 and CC 2021 and can confirm that you are correct. I'm inclined to think this is a bug and that it's been around for a long time.
I base that on the following: If you select some of the text that's been highlighted by the GREP style and try checking the applied character style ID reports it as [None] in both the Control and Character panels, yet if you do a Find/Change (GREP or Text) for the highlight character style it finds the GREP Styled text just fine, even though it still reports as [None]. This seems to be true with Nested Styles as well.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
… So you were wrong! 1 Grep code [just for fun!]:
(?x) \[(?=[^[]*\d) | [^[]*?\K\d(?=[^[]*\]) | (?<!\D)[^[]*\K\]
Good headache!
(^/)
Copy link to clipboard
Copied
The (?x) was a new one on me. Had to look that one up! But this works!
Copy link to clipboard
Copied
Ah yes finding them individually - but you can't find it contiguously - which is what I said.
Yes you can fake it and I was thinking that myself.
The only problem I am getting with the code is the double bracket
Copy link to clipboard
Copied
![]()
I do prefer 3 Grep styles treating each part: the open bracket "[", then the numbers, and finally the close bracket ]", taking in account the context.
\[(?=[^[]*\d+[^[]*\])
\[[^[]*?\K\d+(?=[^[]*\])
\[[^[]*\d+[^[]*\K\]
By @FRIdNGE
![]()
… So you were wrong! 1 Grep code [just for fun!]:
(?x) \[(?=[^[]*\d) | [^[]*?\K\d(?=[^[]*\]) | (?<!\D)[^[]*\K\]
Good headache!
(^/)
By @FRIdNGE
For the record - this is still 3 GREP searches separated by the "|" symbol
But it's an excellent solution - just can't find a way around the double bracket situation, if one exists.
You are an excellent coder - that's some GREP you came up with there! Fantastic.
Copy link to clipboard
Copied
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?? 🙂
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
so far works perfectly well. Thanks again for all contributors.
Copy link to clipboard
Copied
will appreciate however a small info of how does it work, as I am not the master of GREP