GREP search to increment numbers?

Learn / Forums / General InDesign Topics / GREP search to increment numbers?

Viewing 13 reply threads
  • Author
    Posts
    • #54195

      I thought I'd posted this, but it seems to have disappeared. I'm looking for a way to use GREP to autoincrement numbers within a certain range. For example, I might want to add 1 to all numbers (with a specific character style) between 54 and 103, and 2 to all such numbers between 104 and 178. Is there an easy way to do this?

    • #54196
      Eugene Tyson
      Member

      Find

      (53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78
      |79|80|81|82|83|84|85|86|87|88|89|90|91|92|93|94|95|96|97|98|99|100|101|102|103)(?!d+)

      Change to:

      Pick your style

      Repeat for other ranges, but subsitute their numbers in instead.

    • #54198

      Yes, I can get that far, but I don't see how to tell 53 to be 54 and in the same search tell 54 to be 55, 55 to be 56 and so on. And that's a hell of a big search string to type out anyway – is there no way to define a number range?

    • #54199
      David Blatner
      Keymaster

      I don't think you can do this with grep. However, perhaps you could find some way to do it with grep plus this numberadjuster script. Hm… tricky…

    • #54200

      Oo, that script looks useful. It does about half of what I'd need. Now to find some way to specify a number range.

    • #54201
      Eugene Tyson
      Member

      I didn't type them out :D

      I opened up Excel, created a list of numbers

      53

      54

      Dragged the cells down to 103

      Copied to InDesign

      converted table to text

      Grep search

      r

      change to

      |

      Copy and paste text to GREP find

      I'm sure it can be scripted though.

      If you don't have a lot to do then this will work.

    • #54206

      Hank, I don't have the remotest idea how you think it would add 1 to any of these numbers …

      David is right, of course — GREP Is Not Magic. It cannot do calculations, nor can it search an item from a list[*] and replace it with an equivalent from another list. You cannot have a similar list in the replace field.

      [*] Well, it can, using the OR | operator, but “Replace” doesn't know which item it found.

      Also, you can enter a 'range' in the Find field in the form [a-z] — this will find anything from 'a' to 'z'. But GREP does not know maths — you cannot search for [69-82]. (Go ahead, try. You will see it treats this as '6', '9-8', and '2' — and its anybodies guess what it will do with the malformed range in the middle.)

      A script to search and change the numbers in your range could be something like this (a rough attempt but it seems to work):

      //DESCRIPTION: Change some numbers and leave some else. In a selection as well.

      // A Jongware production. No guarantees.

      var ranges = [

      [54, 103, 1],

      [104, 178, 2]

      ];

      // step 0: verify selection

      if (app.selection.length != 1)

      {

      alert (“Well, I warned you to select something!”);

      exit();

      }

      // step 1: find ALL numbers

      app.findChangeGrepOptions = null;

      app.findGrepPreferences = null;

      app.findGrepPreferences.findWhat = “\b\d+\b”;

      numberList = app.selection[0].findGrep(true);

      // alert ('found '+numberList.length);

      // step 2: change certain ranges

      for (a=0; a<numberList.length; a++)

      {

      thisVal = Number(numberList[a].contents);

      for (b=0; b<ranges.length; b++)

      {

      if (thisVal >= ranges[b][0] && thisVal <= ranges[b][1])

      {

      numberList[a].contents = String(thisVal+ranges[b][2]);

      break;

      }

      }

      }

      // step 3: … erm. That's all!

      (David, how to format code?)

    • #54208
      Eugene Tyson
      Member

      Oh shoot – I just thought they wanted to change the style of the text, not to do an addition as well.

      Sorry about that. I'll read the topic more carefully next time.

    • #54212

      Hey, Hank, I did not notice that 'in a certain character style'! ('dans une certaine condition', perhaps)

      Add this line somewhere after the findWhat line:

      app.findGrepPreferences.appliedCharacterStyle = “YourCharacterStyleNameHere“;

      to search for just those numbers. The rest of the script is, unlikely as it may sound, totally unaffected by this change — it merely now finds, and optionally changes, numbers that have the style applied.

    • #54215
      Eugene Tyson
      Member

      Good stuff Jongware, we (and by “we” I mean you), got their eventually (and by eventually I mean you're a fantastic scripter, well done sir).

    • #54216

      I considered using your GREP (so it would have been a full joint effort). But I think it would have been slightly less typework to search for

      b(5[4-9]|[6-9][0-9]|10[0-4])b

      GREP can't do proper number ranges but I can Laugh

      As a side note: I prefer using the Break code b over not-groups. The Break code also works as marker at the start or end of a story or paragraph. Besides, the '+' in (?!d+) is not necessary, and you forgot the equivalent (?<!d) at the start of that long string.

      Is it me, or should all good GREP strings look like the ”$%#$!” one used to see in comics before they went “adult”?

    • #54217
      Eugene Tyson
      Member

      You can't tell from Jongware's picture, but if you were rotate that picture his head goes back about 10 feet to accomodate his huge brain.

      That's fantastic, I'll have to remember that one.

    • #54218
      Eugene Tyson
      Member

      You can't tell from Jongware's picture, but if you were rotate that picture his head goes back about 10 feet to accomodate his huge brain.

      That's fantastic, I'll have to remember that one.

    • #62699
      Erica Gamet
      Participant

      Just wanted to say that this post came in so handy yesterday! Someone asked if I could get them a GREP expression to find all #s between a certain range and add 50 to them. I immediately thought of the Number Generator script (thanks, David B. for introducing me to that one) and Jongware's excellent GREP for finding a range of numbers was invaluable! One of the things I love about GREP is finding an expression that does something very similar, then picking apart the elements to tweak it to my needs. Working with GREP keeps the ol' gr(e|a)y matter awake!

Viewing 13 reply threads
  • You must be logged in to reply to this topic.
>
Notice: We use cookies on our websites to give you a great online experience. If you keep browsing, we'll assume you're ok with this. For more information, see our privacy policy. By closing this banner, you agree to the use of cookies.I AGREENo