Skip to main content
SuzzyFlamingo
Inspiring
September 2, 2025
Answered

Problems with GREP style

  • September 2, 2025
  • 4 replies
  • 1143 views

I have a par style with a grep style to format the last word of the paragraph of that style (hebrew/arabic). I set teh correct char style and then used this grep code:

[[:alnum:]]+[[:punct:]]?$

(Don't think i know grep. I got from ChatGPT)

It absoltlrly refuses to obey. I admonished Chatgpt and is gave some others but they all refuse to commit, Why is ID refusing to follow orders?

 

Thank you
Susan Flamingo

Correct answer Peter Spier
Ok, here is a demo file. The text is Hebrew. You will the final bracket is bold which the charstyle and that's the whole problem.
thank you

Well, this is pretty curious. When I open your sample file here on my US version, everything is fine (I edited the style to make it red for easier identification while testing).

This makes me think there might be a problem in the ME build, in which case no amount of tweaking that GREP expression will make a difference, BUT I'm going to suggest a possible workaround: Add a SECOND GREP Style to your paragraph style for \]$ and use it to apply the [None] character style.

I don't know if it will make any difference, but at least here all of your text shows as US English. Assigning the correct language as part of your Paragraph Style will, if nothing else, enable spell checking, and might have other beneficial effects.

4 replies

Peter Spier
Community Expert
Community Expert
September 3, 2025

Another question...

Is the correct language already assigned to those words, or is the GREP style being used to assign it? Since you seem to have two languages involved, you can't be using the a GREP style to do twolanguage assignments using the same pattern, so I suspect that means the language is already applied.

If that's true, as @TᴀW mentions in at least one other thread, you can use the language assignment, either as the determining factor for a match, or as a qualifier to prevent changes to ther text that would match, but has the wrong language applied.

SuzzyFlamingo
Inspiring
September 3, 2025

Your suggestion of: \S+(?=\[$)

doesn't work. As i think i mentioned, i am working in Hebrew/Arabic, so maybe this is the problem. I think I will have to give up on grep and then simply change the last word at the end of the job via find and replace. So now my question is how do I search for the last word in the paragraph and change its style, and then go change the style of the "]" alone back to the regular"

So I would search for any Word that ends with a ] (no other words like this formatted with that par style) .

How to?

Peter Spier
Community Expert
Community Expert
September 3, 2025
quote

Your suggestion of: \S+(?=\[$)

doesn't work. ...and then go change the style of the "]"


By @SuzzyFlamingo

UMMM.. in your post abpve you said the last character was "[", but now you say "]" which, if that's really what it is, explains why the GREP doesn't work and chaning to \S+(?=\]$) should take care of it unless there's a conflict with the bracket characters being different for Hebrew or Arabic.

It would be useful to select your bracket and open the Glyphs panel to see what character it is. Pasting it in in place of the typed bracket might be necessary if it doesn't come up as the ascii character.

 

Can you post just a snippet of your text? We're really shooting blind here.

Community Expert
September 3, 2025

Those posix alnum and punct are typically only latin characters - from my understanding

 

You probably want “the last run of letters/numbers” but not the final punctuation. You could try:

\S+?(?=[[:punct:]]?\r?$)

That way it matches the last word, but only if it’s followed by an optional punctuation mark and then the end of the paragraph.


Another option:

\S+(?=\s*[[:punct:]]?\s*$)

which is more forgiving if there’s a space before the punctuation.

 

If you can show us some sample text of before and after we'd have a better understanding of what you're after.

SuzzyFlamingo
Inspiring
September 3, 2025

Well, no "before and after" because both don't work (they continue capturing the last character, which is - "[" - and not just the last word without that....bummmer.

What now?

Peter Spier
Community Expert
Community Expert
September 3, 2025

Sorry, missed the part where you don't want to capture the last character. Is it always [? 

if so, try \S+(?=\[$) but this only will work if [ is the very last character, no space or punctuation following.

Peter Spier
Community Expert
Community Expert
September 2, 2025

@SuzzyFlamingo , @jmlevy 's answere should work, but I wanted to recommend to you Peter Kahrel's GREP tutorial: 

https://www.amazon.com/GREP-InDesign-InDesignSecrets-Peter-Kahrel/dp/0982508387

It's comprehensive and actually pretty easy to follow, and you won't be sorry for aquiring your own GREP skills

jmlevy
Community Expert
Community Expert
September 2, 2025

Hi @Peter Spier Absolutely, your book is a must have!

Peter Spier
Community Expert
Community Expert
September 2, 2025

It's not my book, it's by @Peter Kahrel 

He's an absolute wizard.

jmlevy
Community Expert
Community Expert
September 2, 2025

Try this (assuming that there is a final dot at the end of the paragraph): 

\w+\.$

SuzzyFlamingo
Inspiring
September 3, 2025

It did not work for me. I have tried so many suggextions and nothing has worked. I am working in Hebrew/Arabic. 

Finally this did it: \S+?(?=\r|\z) and it worked! (but one thing: this "captures" the last word of the paragraph TOGETHER with the last charachter which is usually a period but sometimes something else. Do you know how to tell it not not to incluse the last char?)

Chat GPT told me that grep styles are very finicky and can get messed up for a lot of reasons. How true is that?

Peter Spier
Community Expert
Community Expert
September 3, 2025

So... 

@jmlevy 's expression says find all word characters (a-z, A-Z, 0-9, and underscore) followed by a period and the end of paragraph. From my reading, it appears that it does not match Hebrew and/or arabic characters. The use of non-Latin characters is an important bit of information.

\S+?(?=\r|\z) matches any non-whitespace characters (shortest match, triggered by the ? after the + sign, but in fact that probably is not doing anything;\S is the negaive of \s which is any whitespace).  followed immediately by a paragraph return or the end of story (\r is the paragraph return, | is "or" and \z is end of story). This expression will also fail if there is any trailing white space at the end of your paragraph (an extra space after the last period, for example). 

Since $ indicates the end of paragraph, even without a return at the end of a story, it is a much more compact way of saying \r|\z .  \S+\s*$ finds all non-whitespace characters along with any trailing whitespaces if they exist, immediately before the end of a paragraph ( the * after the \s means match zero or more occurences, so it picks up paragraph with or without the trailing spaces).

It's not so much that GREP styles are finicky as that GREP expressions themselves can be tricky to design to match all of the text cases you want to find, and only those cases. There are times when it simply isn't possible to do with a single expression, and it is necessary to to use Find/Change with a series of expressions to accomplish the task.