Skip to main content
Known Participant
September 3, 2023
Answered

Increment numbers (preceded by a letter) by 1 in a given range

  • September 3, 2023
  • 1 reply
  • 775 views

Say I have this sequence of letters and numbers:

A200, B45, C38, D400, B800, C44, B304, D2000, B1101, C22, A1234, D420, B2022, C200, B995, A1020.

B4200, C34, B1024, D40.

D200, B11, B975.

I would need to increment by 1 any numbers preceded by the letter B in the --say-- 800-1200 range, as in (in bold type the numbers that were changed):

A200, B45, C38, D400, B801, C44, B304, D2000, B1102, C22, A1234, D420, B2022, C200, B996, A1020.

B4200, C34, B1025, D40.

D200, B11, B976.

Numbers are always preceded by a capital letter. Numbers are always followed by a comma, except for the last number, which is followed by a period. Is this doable?

 

This query is somewhat similar to the following thread:

https://community.adobe.com/t5/indesign-discussions/increment-long-sequence-of-numbers-by-1/m-p/13988339

 

This topic has been closed for replies.
Correct answer brian_p_dts

I'd need to be able to do this by a specific capital letter. That is, I'd need to change the 800-1200 range for B numbers, but I'd need to change the 500-900 range for A numbers. 


Then change \\u to A (or B or C, etc) in the findWhat, then change the range as appropriate 

1 reply

brian_p_dts
Community Expert
Community Expert
September 3, 2023
var doc = app.activeDocument;
app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = "(?<=\\b\\u)\\d+(?=,|\\.)";
var fs = doc.findGrep();
var i = fs.length;
var range = [800,1200];
var num;
while(i--) {
   num = Number(fs[i].contents;
   if (num >= range[0] && num <= range[1]) {
        fs[i].contents = num + 1 + "";
}
}
  

Sure doable. Something like the above. Untested (on mobile)

Known Participant
September 3, 2023

Thanks, but I get an error: 

Offending text: ;

It seems to be the semicolon in 

   num = Number(fs[i].contents;

 

brian_p_dts
Community Expert
brian_p_dtsCommunity ExpertCorrect answer
Community Expert
September 4, 2023

I'd need to be able to do this by a specific capital letter. That is, I'd need to change the 800-1200 range for B numbers, but I'd need to change the 500-900 range for A numbers. 


Then change \\u to A (or B or C, etc) in the findWhat, then change the range as appropriate