9 Replies Latest reply on Aug 7, 2014 1:46 PM by Peter Spier

    GREP #123456 or #123456-78

    isips Level 1

      Hi, I am trying to use GREP to change the character style of numbers that are six digits and follow a pound sign (ie. #123456) or six digits that follow a pound sign and are followed by a dash and two more digits (ie. #123456-78).


      I have looked for information on GREP styling in indesign but I can't really find anything that's more than "5 cool things with GREP" or "10 ways GREP can help". Can anyone suggest anything more comprehensive?

        • 1. Re: GREP #123456 or #123456-78
          isips Level 1

          I figured out the GREP for #123456 and #123456-78 (seems to be #\d\d\d\d\d\d\d and #\d\d\d\d\d\d\d-\d\d)


          But, still, does anyone have suggestions for reference material for GREP

          • 2. Re: GREP #123456 or #123456-78
            Peter Spier Most Valuable Participant (Moderator)

            If you want to include the # sign I would use #\d{6}(-\d{2})*(?=\s) which is a little more compact (the numbers in {} tell how many you need to match, the (-\d{2})* says it should match the # plus six digits and the dash plus two more if they are there, but they don't need to be, and the (?=\s) says whatever is matched needs to be followed by some sort of whitespace which prevents matches to other odd strings that are part of a longer string. You can add a lookbehind for whitespace as well as long as your # sign isn't the first character in the story. In that case there would be no whitespace before and there would be no match for the first number.


            Best reference for GREP I know is GREP in InDesign - O'Reilly Media

            • 3. Re: GREP #123456 or #123456-78
              Obi-wan Kenobi Adobe Community Professional

              Hi Peter,


              Very good, but not sure (?=\s) is wise. If we consider we only find #xxxxxx or #xxxxxx-xx, I propose:





              • 4. Re: GREP #123456 or #123456-78
                Peter Spier Most Valuable Participant (Moderator)

                Not sure if I tried \b only at the end, but it didn't work for me when I tested both ends (probably because ID doesn't consider # to be a word character), so I switched to (?+\s) on the theory that it would equally define the end of a string. The only place it might fail that I can think of would the the end of a story (just as the lookbehind fails at the beginning).


                Basically, I think without seeing exactly how the text is formatted in the document it's going to be pretty difficult to write an expression that is perfect.

                • 5. Re: GREP #123456 or #123456-78
                  isips Level 1

                  Hey, thank you very much to you both. In my context, these numbers are Part Numbers and, in the paragraphs in which I GREP for them, they will always follow the part. In other words, they will never be first in a story.


                  However, they are often followed by a break character so would (?=\s) not work in that case? I will test it in the meantime.

                  • 6. Re: GREP #123456 or #123456-78
                    isips Level 1

                    Yup, so /s is looking for any non-hidden character basically? is there a term for those?

                    • 7. Re: GREP #123456 or #123456-78
                      Obi-wan Kenobi Adobe Community Professional



                      Can you show us a real layout? … and we'll fix it! 

                      • 8. Re: GREP #123456 or #123456-78
                        isips Level 1

                        Oops, I meant to say that it was working. I changed my formatting to #\d{6}(-\d{2})*(?!\d) because (?=\s) restricts the strings from being followed by punctuation. I only want the formatting to deactivate when the string has too many numbers, I don't care if it is followed by a letter. Thanks for the help.


                        I do have a question: What are the OR and AND operators in GREP?

                        • 9. Re: GREP #123456 or #123456-78
                          Peter Spier Most Valuable Participant (Moderator)

                          If you want to restrict this so it doesn't find parts of strings with too many numbers, but you can accept anything else, then use #\d{6}(-\d{2})*(?=\D|$)  The \D means "not a digit," | is the "or" and $ in this case means the end of story marker, so (?=\D|$) means find the number followed by anything at all that is not a digit, or by nothing at all (the end of story marker). Remember, this will also find the first part of #123456ABCD, not just your numbers followed by space or punctuation.


                          As mentioned above the pipes (|) is the or operator. There is no "and" operator other than defining the expression to have all of the criteria you need to match (which usually involves some sort of lookahead or lookbehind, or a scope or formatting limitation added in the search dialog.