Copy link to clipboard
Copied
I want to write a GREP query to put the cursor right before a word in a Numbered/Bulleted List.
The Numbered/Bulleted List does not have a Paragraph Style applied.
For example, a numbered list like this:
1. Adam
2. Bryan
3. Chris
4. David
The GREP Find query should jump the cursor right before Adam so that I could replace(insert) something there.
Find Next jumps the cursor right before Bryan, and so on.
1 Correct answer
Thanks Peter,
So with your authorization, here's what you wrote:
...
Inserting thousand separators, to change 12345678 to 12,345,678 (or use any other symbol, such as a dot or a thin space) could be handled with one query (this one is from Friedl’s Mastering Regular Expressions, p. 67):
Find what: (\d)(?=(\d\d\d)+\b)
Change to: $1,
(You can change the comma following $1 to your separator character or space of choice.)
I wrote “could be handled with one query”, because if the numbers you want to process a
Copy link to clipboard
Copied
Set 'Bullet list/Numbers' in the Find Format field. Then look for ^.
That selects the first character in numbered paragraphs. Unfortunately, because of a bug you can't use Find Next when you look for just ^ hoping to place the cursor after the number.
P.
Copy link to clipboard
Copied
Thanks, Peter for the answer.
I used the GREP query:
Find what: ^(?=.)
Find Format: Numbered
to insert cursor right before the 1st word in the Numbered List. That's great.
However, as you said, Find Next did not take me to right before the next word in the list.
Any pointers on how that could also be done will be wonderful.
Copy link to clipboard
Copied
Using the GREP query, I can place the cursor right before the word.
However, if I want to insert something at that location by inputting a replacement string in the Change to: field (example: 'Mr. ' so that the entry in the list becomes Mr. Adam), it does not work (the cursor is a static cursor, not blinking as it should have been) - that's a bug.
I tried another example: If we want to input comma at the thousandths place in this long number 254586920.4 so that it looks like 254,586,920.4 by writing a GREP query:
Find what: (?<=\d)(?=(\d{3})+(?!\d))
Change to: ,
here too it does not work. The Find what works and cursor jumps to the correct location, however, Change to: fails to put the comma separator there.
Any hints how this could be achieved?
Copy link to clipboard
Copied
Cupertino Fruit wrote
to input comma at the thousandths place in this long number 254586920.4 so that it looks like 254,586,920.4
Find what:
\d{3}(?!\D)
Change to:
$0,
What effectively means: insert a comma after any three-digit group if it is followed by a digit
Copy link to clipboard
Copied
Hi Winterm,
I'm afraid that would not work because 12345 would become 123,45
There's a solution in Peter's book (p.57) but I'm not sure I have the right to reproduce it here.
Besides, I must say I'm not too happy with it (no offense Peter ) and I'm hoping there must be a better way. I'll investigate...
Copy link to clipboard
Copied
Hi vinny, nice to see you
Sure, since it's a quickie, based strictly on OP's given sample. Mostly, it won't work without decimals delimited with a dot. To take this more seriously one could look for a good script, like the one by Ariel Walden, (c) www.Id-Extras.com, 2014.
Copy link to clipboard
Copied
> There's a solution in Peter's book (p.57) but I'm not sure I have the right to reproduce it here.
Sure you can!
> Besides, I must say I'm not too happy with it (no offence Peter ) and I'm hoping there must be a better way. I'll investigate...
I'm all ears. . .
P.
Copy link to clipboard
Copied
Thanks Peter,
So with your authorization, here's what you wrote:
Inserting thousand separators, to change 12345678 to 12,345,678 (or use any other symbol, such as a dot or a thin space) could be handled with one query (this one is from Friedl’s Mastering Regular Expressions, p. 67):
Find what: (\d)(?=(\d\d\d)+\b)
Change to: $1,
(You can change the comma following $1 to your separator character or space of choice.)
I wrote “could be handled with one query”, because if the numbers you want to process are in tables, then the above expressions won’t work. It’s not entirely clear to me why, but I suspect it’s because of the \b word-boundary code which doesn’t match the \Z end-of-story code (each table cell is a separate story) [...]
Well, this table issue bothered me. And I should add that if the number is at the end of a story, it wouldn't be caught either...
(Or more exactly, the regex will just catch the first digit if it's followed by a multiple of 3 digits)
Sooo... I've been scratching my head about this nice "thousand separators" Grep puzzle...
And I think I might have broken it...
What about
Find (\d{1,3})(?=(\d{3})+\b)
Replace $1,
Would that work for you?
FYI, and to be completely honest, I don't really understand what's going on there...
I was testing (\d{1,3})(?=(\d{3})+) but for some reason it would only catch the first 3 digits for a start, regardless of whether or not they were followed by a multiple of 3 digits... I felt that I needed some kind of "break" but couldn't find it. Then I tested \b... and... it works.
I must keep investigating...
ps: nice to see you too Winterm
Copy link to clipboard
Copied
Excellent! The only real difference between your and Friedl's expressions is the first part (the second part, the lookaheads, are just notational variants). It's not clear to me why yours -- (\d{3}) -- works and Friedl's/mine -- (\d) -- doesn't. But anyway, I've made a note and will have it changed in the next edition.
Thanks,
P.
Copy link to clipboard
Copied
Well, you know what...
I don't get it either (And I was secretly hoping for a GREP guru to come up with some explanation :-))
After further investigation, nope... still don't get it.
Tested both regex with other GREP tools, and they act the same way, as expected...
Only in Indesign they act differently, for some mysterious reason... Frustrating...
Btw, I think we have digressed from the original question.
I think the correct answer to it should probably be
Find ^(.)
Replace Mr $1
All the best
Vinny
Copy link to clipboard
Copied
How can we make this work for (any number of) digits after a decimal so that the thousandth separator is ignored there?
Example: 2587468.5784
The above suggestion changes it to 2,587,468.5,784 (which is obviously incorrect).
Copy link to clipboard
Copied
Good question.
In theory, adding a negative lookbehind saying "if not preceded by a dot then zero or more digits" should do.
(?<!\.\d*)(\d{1,3})(?=(\d{3})+\b)
Buuuut... Indesign doesn't accept quantifiers with lookbehind.
There's a workaround for positive lookbehinds that you may be aware of: \K
Unfortunately, I am not aware of such a workaround for negative lookbehinds...
If anyone is... please share the knowledge.
That said, I was testing the following regex: (which btw was not good enough because it would only ignore 4-digits decimals)
(?<!\.)(\d{1,3})(?=(\d{3})+\b)
aaaaand... I made a noobs mistake: I forgot to escape the dot
Which led me to this expression:
(?<!.)(\d{1,3})(?=(\d{3})+\b)
And you know what?
It seems to be achieving what we want.
And I don't have any idea why, which is btw the second time in this thread.
In North of France, where I'm from, there's a popular sweet called "bêtise" which means blunder, because the recipe was found after a stupid mistake.
Would this "Grep bêtise" work for you?
Copy link to clipboard
Copied
Of course, it works! You're being very modest.
But now does not work for negative numbers, wonder why?
Thanks so much!
Copy link to clipboard
Copied
Ah!
I think I found a workaround for dealing with quantifiers and negative lookbehind:
We can create the character class [.\d]
Looks like inserting a character class within a negative lookbehind acts like some sort of quantifier.
It still is some sort of Indesign Grep mystery but it makes more sense.
So, would this regex be the ultimate thousands separator ?
(?<![.\d])(\d{1,3})(?=(\d{3})+\b)
By the way, it still should be handled carefully and not as a "replace all" regex because there are many cases where you don't want to replace numbers:
You don't want "In 2018, I earned 2018€/month" to be changed into "In 2,018, I earned 2,018€/month"
Copy link to clipboard
Copied
Since you cannot add something at insertion point, you still can catch the first character and add something in front of it:
Find ^(.)
Replace Mr $1

