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

(GREP) formatting product numbers

Community Beginner ,
Mar 18, 2022 Mar 18, 2022

I am currently trying to format some numbers in a table. I am doing this using GREP in a paragraph style that's applied to the numbers. My problem is that the numbers have so many special situations.

The goal is that the numbers I marked blue in the screenshot do not have to be formatted manually anymore.

The bold parts of the numbers are already being formatted with GREP.

Bildschirmfoto 2022-03-18 um 15.43.06.png

 

 

 

 

 

 

 

Currently, this is the code I tried for formatting the blue numbers.. but it is not working.

(?<=-)(\d{2,3})(!\d)(!\.)

Arguably, my GREP skills are not that great.

Do you guys have any ideas what I coud try?

TOPICS
Scripting
2.6K
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 ,
Mar 18, 2022 Mar 18, 2022

Your sample looks like "select any two digits that are preceeded by either a period or dash, and that are not followed by additional digits". Your regex seems to imply that you'd take any two or three digit number, right? 

 

 

(?<=\.|-)(\d{2,3})(?!\d)

 

 

First: a positive lookbehind, "capture anything that follows a literal period or a dash".

Then: a group of "any two or three digits".

Followed by: a negative lookahead, "don't capture anything that doesn't precede a digit".

 

There weren't any three-digit valid groups to be captured in your sample, so I'm not sure why you need that, but I made some "-00XS to -999XS" examples to make sure this regex caught 'em. 

 

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 ,
Mar 18, 2022 Mar 18, 2022

Sorry, I had some copy-paste confusion over here, my previously posted negative lookahead is incorrect.

 

This matches your sample but it feels wrong, somehow:

(?<=\s|\.|-)(\d{2})(?=,|\>|\u)(?!\.)

It finds two-digit numbers preceded by whitespace, literal periods, or dashes, and followed by commas, uppercase letters, or end-of-paragraph locations, but not when follwed by periods. Even though your attempt at a regex included three-digit numbers, I've excluded them here because your sample doesn't include any three-digit numbers to capture. 

 

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 Beginner ,
Mar 21, 2022 Mar 21, 2022

Dear Joel,

thank you so much for your reply. Your description of the code really helped me understand it.

 

I tried the code, but unfortunately it is not highlighting every number, just the ones at the end. I am not sure why, all of the lookbehinds and lookaheads should be correct. I just added a whitespace to the lookbehind.

(?<=\s|\.|-)(\d{2})(?=,|\>|\u|\s)(?!\.)

Do you know what could be the reason for the code not catching all of the numbers?

Bildschirmfoto 2022-03-21 um 15.02.17.png

 

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 ,
Mar 21, 2022 Mar 21, 2022

Well, I don't think that the addition of the whitespace should change anything. Also, when I use your regex with your added whitespace, it still catches all the same groups as it did before. Similarly, when I try your regex on my install of InDesign, it doesn't look like your most recent posting at all. So you may want to check your expressions on your side. 

 

Is there a reason that you're adding that space? You could also use the end-of-paragraph marker ($ without these parentheses), if you're trying to treat that pilcrow end-of-paragraph glyph as whitespace.  It's just a location marker, it acts like a positive lookbehind. Similarly, you might want to think about using beginning-of-word (\< without parentheses) and end-of-word (\> without parentheses); it saves a lot of time trying to figure out all the possible permutations - you know, is it bound by a whitespace, a comma, a semicolon, a colon, an etc etc etc. 

 

Lastly, I think that Mike has some really good advice, here. I often feel like trying to catch every possible permutation with ever-more-complex regular expressions is like trying to catch rain with a net. I mean, you can do it 🙂 but there might be an easier way. 

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 Beginner ,
Mar 28, 2022 Mar 28, 2022

I really don't know why the code is not working, I even tried it in a second, new document but it still does not catch all of the numbers it should catch. Can you show me a screenshot of your GREP window?

Here is what mine looks like, I hope none of the other GREPs are interfering with this one.. but even when I delete them, it still does not work.

 

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 ,
Mar 21, 2022 Mar 21, 2022

Have you considered defining the paragraph style to bold everything, and aftewards use GREP styles to set relatively few expressions like "VAR" to a character style that forces it to Regular?

Mike Witherell
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 Beginner ,
Mar 28, 2022 Mar 28, 2022

That is actually what I am doing right now.

This is how all of my GREPs for these product numbers look like:
(The AR numbers are also used sometimes, although not in the examples I posted here)

 

Bildschirmfoto 2022-03-28 um 12.05.12.png

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 ,
Mar 22, 2022 Mar 22, 2022

Hi @mira_00 

Is that really what you want? Or is that a mistake?

 

aaa4.png

 

 

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 Beginner ,
Mar 28, 2022 Mar 28, 2022

That is not how it should look like, you are correct. The "-" should be included in every case.

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 ,
Mar 28, 2022 Mar 28, 2022
quote

That is not how it should look like, you are correct. The "-" should be included in every case.


By @mira_00

 

And what is with the "*" and the "." ?

Always black and bold? Or not?

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 ,
Mar 22, 2022 Mar 22, 2022

Unfortunately, I don't know what exactly your final goal is (see my last post). But I would go the direct route: Format only what needs to be formatted. Instead of formatting too much first and then formatting parts again.

 

That would be possible directly, for example:

 

aaa5.png

 

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 ,
Mar 28, 2022 Mar 28, 2022

In my opinion you only need

  • one Paragraph Style
  • two Grep Styles in that Paragraph Style and
  • two Character Styles

 

But it may well be that I am wrong.

Because I'm still not quite sure what the end result should look like. There is still some information missing from you.

 

Give it a try:

Above you can see the two grep styles (individually).

Below you see the combined result and your original image.

In the middle you see the two grep styles.

 

Grep_Digits.png

 



[ edited by pixxxelschubser ]

Oops. I overlooked a small detail. But now I'll wait for your answer.

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 Beginner ,
Mar 29, 2022 Mar 29, 2022
LATEST

Hi pixxxelschubser,

wow, thanks so much for your reply! I tried out the code and it worked 🙂

However, I did not get it to work with only two GREP styles, I had to add a third one.

 

Not sure why, but my texts did not look like the ones on your screenshot with only two GREPs. The blue numbers failed to format correctly. It looks like this, only two cases are recognized by the code:

mira_00_0-1648556072516.png

So right now, I made an overarching paragraph style that formats everything to blue and bold, and three character styles: Regular, Bold and Bold with Color.
This way, it is working. But I think it's messier than your approach. What could be the reason it is not working the way you do it?
Here is the code and a screenshot of my text.

Bildschirmfoto 2022-03-29 um 14.07.04.png

Bildschirmfoto 2022-03-29 um 14.07.24.png

Regarding your question from above: It is okay for me for the "-" and "." to be included in the blue formatting everytime, as long as it is consistent.
The "*" can also be bold.

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