1 2 Previous Next 44 Replies Latest reply on Jul 21, 2014 11:33 AM by claidheamdanns

# Change number format with GREP?

Hi,

I'm sure this is easy, but I can't get my head around it.

I've got a large table in ID4. I need to change all the format of all numbers over one thousand.

So 1234 becomes 1,234 and 12345 becomes 12,345, but it leaves all smaller numbers (ie up to three digits) as they are.

I've tried a few things, but they all fail to deliver teh results I need

Help!

• ###### 1. Re: Change number format with GREP?

I'd think you'd have to do this in stages.

What happens when you get to 1,234,567 - you'd need multiple commas to go in.

For 4 digit numbers

\<(\d)(\d{3})(?!\d)

Change to

\$1,\$2

5 digit numbers

\<(\d{2})(\d{3})(?!\d)

Change to

\$1,\$2

6 Digit Numbers

\<(\d{3})(\d{3})(?!\d)

Change to

\$1,\$2

7 Digit Numbers

\<(\d)(\d{3})(\d{3})(?!\d)

Change to

\$1,\$2,\$3

8 Digit Numbers

\<(\d{2})(\d{3})(\d{3})(?!\d)

Change to

\$1,\$2,\$3

• ###### 2. Re: Change number format with GREP?

Maybe something like this:

Find what: (?<!\<)(?<![\,\.])\d{3}\>

Change to: ,\$0

Run multiple times to get everything changed.

--

Marijan (tomaxxi)

http://tomaxxi.com

• ###### 3. Re: Change number format with GREP?

Peter Kahrel suggests in his book the following (\d)(?=(\d\d\d)+\b) and replace with \$1,  which works everywhere EXCEPT in tables if the number is the last thing in the cell. His workaround for tables is to do a three-step change:

Find any digit at the end of the story (\d)\Z and add a thin space \$1~|

Find a thin space at the end of a story ~|\Z and remove by leaving the change field blank.

Peter's book was one of the best \$10 investments I've ever made.

• ###### 4. Re: Change number format with GREP?

That particular one fails at this point

12

123

1,234

12,345

123,456

1,234,567

12,345,678

1,234567890

• ###### 5. Re: Change number format with GREP?

Marijan's GREP just keeps on going!

12

123

1,234

12,345

123,456

1,234,567

12,345,678

1,234,567,890

12,345,678,901,234,567,890

It's very clever because it looks from 3 digits from the end of the digits.

Way too clever

• ###### 6. Re: Change number format with GREP?

Yeah, it's pretty handy

Only downside is that you have to run it multiple times.

• ###### 7. Re: Change number format with GREP?

Eugene Tyson wrote:

That particular one fails at this point

12

123

1,234

12,345

123,456

1,234,567

12,345,678

1,234567890

Actually, this looks like a case similar to the table. It fails on the last number IF it is the las thing in the story, but if there is anything following it works, so the three step method would again be suitable (and could be chanied as a script).

I'll have to try Marijan's code...

• ###### 8. Re: Change number format with GREP?

Thanks very much Marijan, that worked perfectly.

If you have got a couple of minutes spare, could you explain how it works so that I can try to learn  a bit more about using GREP efficiently?

I don't use it very often, but I'm trying to build up a basic working knowledge

Thanks

• ###### 9. Re: Change number format with GREP?

Part 1

(?<!\<)

(?<!) is the code for a negative lookahead - effectively means it's not looking for this

\<  indicates that it's the start of a word

So it's not looking for the start of the paragraph - i.e., ignore it

Part 2

(?<![\,\.])

(?<!) same as above [ignore]

[\,\.] \, - is ro represent a "comma" and \. is to indicate a "full stop". To input individual punctuation like "," "." ";" etc. you need to put a "\" (known as an escape).

The square brackets around this "[ ]" means a "character set".

Basically instructing the search to "ignore" any instance of encountering the character set of a "comma" or a "full stop".

Marijan - I just tried this code without this in it - and it still works - I think this is because it's already looking at the end of the Word - and a comma breaks it into spearate words. I  don't think this part is necessary?

Part 3

\d{3}\>

\d = digit

{3} = 3 digits in a row

\> = at the end of the word

Essentially the instruction is

(?<!\<) Don't look at the start of the word (?<![\,\.]) ignore all the commas and and full stops \d{3}\> find all the 3 digits at the end of the word

The change code means

,\$0

, = comma

\$0 = found text

Basically = Insert a "comma" followed by all the found text

(punctuation does not need an escape "\" in the Change)

When the Search finds

ALL 3 digits at the end of a word that contains 3 DIGITS it will replace all the 3 digits with itself - preceded by a comma.

It's very clever.

• ###### 10. Re: Change number format with GREP?

(?<!\<)\d{3}\>

Take this text for a sample to work with

1

12

123

1234

12345

123456

1234567

12345678

123456789

1234567890

Starting at Part 1

And put in Find

\<\d{3}

It will find all the starting 3 digits

as:

\< = start of the word

\d = any digit

{3} = 3 of those please

Now what you want to do is ignore anything at the start of the word

The way you ignore something in GREP is to use the code for a "Negative Lookahead" (there also "positive" and "negative" "lookahead" and "lookbehind")

The code for a Negative Lookahead is the (?<!)

To ignore the Start of the word you include that code inside the Negative Lookahead like so:

(?<!\<)

Run a Search with your code like this

(?!\<)\d{3}

You'll see that it finds all 3 digits in sequence, but not the 3 digits at the start of the word, only 3 digits after the start of the word.

Now you want it to only find 3 digits at the end of the word where the code for this \>

(?!\<)\d{3}\>

Now you're searching for only the 3 digits at the END of the word, but not at the start of the word.

CHANGE TO "Comma" + FOUND TEXT

,\$0

And now it's only searching the END of a WORD it MUST CONTAIN 3 DIGITS and it CAN'T BE AT THE START OF THE WORD

And it's inserting a COMMA at the start of any text that matches the FOUND text.

• ###### 11. Re: Change number format with GREP?

\d = digit

{3} = find this many (curly brackets {#})

\< = start of word

\> = end of word

• ###### 12. Re: Change number format with GREP?

Great breakdown Eugene!

Yes, it looks like that (?<![\,\.]) is not really needed.

• ###### 13. Re: Change number format with GREP?

It's a fantastic GREP find, Marijan. I would never have thought to look for the 3 digits at the end of the word.

It just works. And I've saved it in my GREPs. I will definitely need this

Thank you.

• ###### 14. Re: Change number format with GREP?

I just "invented" it when Colin asked!

• ###### 15. Re: Change number format with GREP?

You are welcome Colin!

Eugene did an awesome breakdown of this GREP!

Hope it helps you understand it.

• ###### 16. Re: Change number format with GREP?

Thanks Eugene,

I get the feeling I'll have to read through this a few times to get my head around  exactly how it works, but I can certainly see the process thanks to your explanation.

I'll try going through it step by step and see the results as I go, as that usually helps.

Cheers everyone! Hopefully this discussion will help a few more people in the future.

• ###### 17. Re: Change number format with GREP?

Until I went back and found Peter K's solution that does all lengths at once,  I was initially going to suggest what seems to me to be the simpler (?<=\d)(\d{3})(?!\d) which also finds the last three digits of a four or more digit number.

Granted, it will find strings of digits that are part of a word like a part number, but for most purposes I would think it should be OK if you are careful about what you select for scope.

I tried writing the script to chain the three queries above, but not being a scripter I spent an hour trying to fake it based on the find/change by list script and got nowhere. It shouldn't be that hard...

• ###### 18. Re: Change number format with GREP?

Maybe I wasn't entirely clear about what it finds which is the last three of at least four digits in any string (three digits preceded by one digit and followed by anything except a digit), so that when you run it multiple times it does what Marijan's code does. Once the comma is inserted in front of a found group of three digits, it gets ignored on the next run because there is no longer any digit in font of the group.

As far as I can tell, the only difference in results between my expression and Marijan's is his will not find 12345abc and mine will. Both seem not to care about non-digit characters at the start of the string (which is good since you might want this to work with currency).

• ###### 19. Re: Change number format with GREP?

http://carijansen.com/2009/07/05/tip-089/

My comment April 13th, 2010

• ###### 20. Re: Change number format with GREP?

By the way, ALL the queries that have been proposed so far also add separators to the decimal part of a four-plus place decimal. I geuss the way to stop that would be to go back to breaking the decimal portion apart, first, then put it back to gether after adding separators.

• ###### 21. Re: Change number format with GREP?

I have concluded that writing a perfectly formatted GERP in InDesign is an art and requires a pure genius.

Could someone help me with the formatting of this one, please?

I started using -\d?,\d?\%|-\d+?% to format all "-" numbers expressed as a percentage (to turn them all red). I then ran into some negative numbers with decimals, so I switched it up and went with this one: -\d+?.\d+?

Now I have some -\$1,234 numbers that I also need red!

Is it possible to have a one-size-fits-all GERP that formats EVERYTHING to the right of (and to include) the negative sign?

So for example:

-1 gets turned to red

-1.1 gets turned to red

-1.12233444 gets turned to red

-\$1,123,123,123 gets turned to red

-1% red

-1.2% red

et al...

I'm struggling and currently using a bunch of wildcards in a find and replace, but a one-size-fits-all GERP solution would absolutely rock!

• ###### 22. Re: Change number format with GREP?

try inserting \\$? or \\$* after the -  (the \$ needs the \ to make it a literal, and the ? or * should make it work 0 or 1 or 0 or many times, respectively)

• ###### 23. Re: Change number format with GREP?

Hi,

Why not a grep style with:

-\S+(?=,\s)|-\S+(?=\.\s)|-\S+(?=\)\s)|-\S+(?=\s)

… where you add other occurrences?

• ###### 24. Re: Re: Change number format with GREP?

hey Obi-wan,

why not

-\\$?(\d{1,3}[,.%]?){1,}

• ###### 25. Re: Change number format with GREP?

Danke sehr! Ich schulde dir einen großen Bier! (ist das richtig? Meine Frau ist Deutsch and ich habe fur zehn Jahren im Deautchland gewont, but can't spell to save my life). lol

I am still playing with it, but that solution works like a champ, Pixxel! It does format phone numbers and other instances that use a hyphen (e.g., -123 vs. 555-1212), but someone linked to a great "GREP QUICK REFERENCE CHART" that has pages of syntax, so this has been a great learning experience and I'm playing with it now trying to set it to only format when the hyphen is the leading character and not in the middle of the string.

Thanks again, sir.

• ###### 26. Re: Change number format with GREP?

And thanks to everyone else, too...dissecting the suggestions using the quick sheet to understand how you constructed them. I really need to get a command of these, as they come in handy A LOT!

Thanks again.

• ###### 28. Re: Re: Change number format with GREP?

Gern geschehen, und danke für das virtuelle Bier.

A short translation for the grep (in german, this is easier for me ):

-\\$?(\d{1,3}[,.%]?){1,}

- ein Minus

\\$? ein Dollar-Zeichen null oder ein Mal

(\d{1,3}[,.%]?){1,} Fundstelle1 beliebige Ziffer ein bis drei Mal gefolgt von Zeichensatz mit Komma oder Punkt oder Prozentzeichen Zeichensatz null oder ein Mal Fundstelle1 ab ein Mal aufwärts

Viel Spass noch beim greppen

• ###### 29. Re: Change number format with GREP?

Hi Pixxxel,

Mine is not perfect (quickly written) but be careful!

• ###### 30. Re: Change number format with GREP?

Hi Obi-wan,

it's always a nice playing with you.

Can you say me please – how is the correct english gramma for:

Science-Fiction

Know-how

And yes, a point or a comma behind a leading digit with a following space is a problem in my grep. (But not the currency signs, you can use  the placeholder "." instead or the class [€£\$]

regards

• ###### 31. Re: Change number format with GREP?

Nice for me too!

I've seen the problem on my grep. So, I've modified it.

((?<!\S)|(?<=\())-\S+[^,;:\.!\?\s\)]

But I think your approach is better. Just fix the problem of the point/comma with a following space.

I go to sleep! If you fix it, cool, tell me!

• ###### 32. Re: Change number format with GREP?

Hi Pixxxel,

Basing on yours, I don't think it is the best grep code I've written but it works in a lot of cases (apparently):

1:     (((?<!\w)-[\\$£€]?([\d,.%]?)+((?=(,\s))|(?=(\.\$))|(?=(\.\s))|(?=(\.\$))))|((?<!\w)-[\\$£€]?( [\d,.%]?)+))

On complex grep, what I do is inserting two other grep styles. They allow you to have a best management of the treatment:

2:    ~j~j.+~j          // inserting 2 NJ before and 1 NJ before allows you to apply the char style.

3:    ~j~j~j.+~j          // inserting 2 NJ before and 1 NJ before allows you to not apply the char style.

marking is fast by selecting the portion of text, that resists to the grep 1 or is unjustly taken into account, and using a small [JS] associated with a keyboard shortcut.

It's a matter of TIME: If your grep, quickly written, finds 95% of the occurrences, I always thought it was not interesting, as it is stupid, to spend hours to look for a code that finds ALL occurences!

In any case, is's my point of view!

• ###### 33. Re: Change number format with GREP?

Hi Obi-wan,

I don't think, that my grep can be fixed. The point and the comma are to deep inside. And every long grep will be slow and slower in long texts.

You know, this is only a theoretical discussion between us. IMHO it's the best way to use a grep like this:

(?<!\d)-.?([\d.,]?)+\>%?

• ###### 34. Re: Change number format with GREP?

Hi Pixxxel,

I made a summary of our different greps. Discussion although theoretical but very informative! [ I appreciate it!  ]

• ###### 35. Re: Re: Change number format with GREP?

Funny!

You've modified my Grep. But without the "end of word \>" my grep does not work properly!

Look at my Grep post#33 without any changes:

IMHO this is Score: 0

See you later

• ###### 36. Re: Change number format with GREP?

Not funny! WEIRD!

That's what I get:

• ###### 37. Re: Change number format with GREP?

That's odd!

Here again - complete in a new document:

• ###### 38. Re: Change number format with GREP?

Pixxxel,

You don't use ID CC?

• ###### 39. Re: Change number format with GREP?

Obi-wan,

no I use ID CS5.0 (version 7.0.3.535) under Windows.

But I prepared an IDML file for you: