Using GREP to select a number range

Learn / Forums / General InDesign Topics / Using GREP to select a number range

Tagged: 

Viewing 5 reply threads
  • Author
    Posts
    • #75009
      Jahrod
      Member

      I thought this would be simple (and probably it is) but after searching the web for no less than an hour, I can not figure out how to get GREP to select a number range. What I would like GREP to do is select all numbers from 1 to 49 so I can colour them with a certain colour using a character style built into my paragraph style. Then I also want to colour numbers 50 to 59 with another character style. Does anybody know how I might be able to do this?

    • #75013
      Wosven S
      Member

      Perhaps there’s a more elegant solution, but an easy way is :

      First, you can color all numbers :
      GREP style color 1-49
      \d

      Second, you color from 50 to 59 :
      GREP style color 50-59
      5\d

      If needed for 60+ :
      GREP style color 60+
      [6-9]\d|[\d]{3,}

    • #75020
      Jahrod
      Member

      @Wosven, this is absolutely PERFECT – thank you! I actually need a code for 60+, so thanks for dropping that one in too :)

      Out of curiosity, how exactly does that last code work? Possible for you to break it down on what each part does?

      Many thanks

    • #75022
      Masood Ahmad
      Member

      This GREP code is a combination of two codes with an IF condition using a Pipe sign (|)

      [6-9]\d means any digit from 6 to 9 followed by any digit
      = This code will select/search all the numbers starting with 6 to 9

      | pipe sign means OR (a kind of if condition)

      The second part [\d]{3,} will select all the digits with a minimum of 3 digits

      It means that this code will select all the numbers…

      Instead if you need to select the numbers from 60 to 99 then you can tweak the @Wosven code to:

      [6-9]\d
      but keep this in mind that this code will also search/select the numbers in between bigger numbers, therefore, you have to look for the numbers that are not preceded by a digit:

      (?<=[\D])[1-4]\d
      (?<=[\D])5\d
      (?<=[\D])[6-9]\d

      Similarly if you want to restrict your search to a two-three digit search string then you can add an additional code, like: {1} or {2} or {3}

      (?<=[\D])[1-4]\d{2}
      (?<=[\D])5\d{2}
      (?<=[\D])[6-9]\d{1}

      Hope this will help

    • #75026
      Jahrod
      Member

      Masood Ahmad, amazing – thank you so much for the thorough breakdown! Saving me so much time already :)

    • #75027
      Masood Ahmad
      Member

      I’m glad, I could help… :)

Viewing 5 reply threads
  • You must be logged in to reply to this topic.
>