Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

GREP query for finding Numbered/Bulleted list

Explorer ,
Jan 14, 2019 Jan 14, 2019

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.

2.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Jan 15, 2019 Jan 15, 2019

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

...
Translate
Community Expert ,
Jan 14, 2019 Jan 14, 2019

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 14, 2019 Jan 14, 2019

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 15, 2019 Jan 15, 2019

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Jan 15, 2019 Jan 15, 2019

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jan 15, 2019 Jan 15, 2019

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...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Jan 15, 2019 Jan 15, 2019

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 15, 2019 Jan 15, 2019

vinny38

> 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jan 15, 2019 Jan 15, 2019

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 15, 2019 Jan 15, 2019

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jan 15, 2019 Jan 15, 2019

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 25, 2019 Jan 25, 2019

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).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jan 25, 2019 Jan 25, 2019

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 25, 2019 Jan 25, 2019

Of course, it works! You're being very modest.

But now does not work for negative numbers, wonder why?

Thanks so much!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jan 25, 2019 Jan 25, 2019
LATEST

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"

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jan 15, 2019 Jan 15, 2019

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines