GREP search to increment numbers?
Learn / Forums / General InDesign Topics / GREP search to increment numbers?
- This topic has 13 replies, 5 voices, and was last updated 11 years, 8 months ago by Erica Gamet.
-
AuthorPosts
-
-
December 10, 2009 at 4:18 pm #54195Rhiannon MillerMember
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?
-
December 10, 2009 at 4:28 pm #54196Eugene TysonMember
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.
-
December 10, 2009 at 4:31 pm #54198Rhiannon MillerMember
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?
-
December 10, 2009 at 4:31 pm #54199David BlatnerKeymaster
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…
-
December 10, 2009 at 4:37 pm #54200Rhiannon MillerMember
Oo, that script looks useful. It does about half of what I'd need. Now to find some way to specify a number range.
-
December 10, 2009 at 4:42 pm #54201Eugene TysonMember
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.
-
December 10, 2009 at 9:20 pm #54206Theunis De JongMember
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?)
-
December 10, 2009 at 9:37 pm #54208Eugene TysonMember
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.
-
December 11, 2009 at 11:28 am #54212Theunis De JongMember
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.
-
December 11, 2009 at 4:27 pm #54215Eugene TysonMember
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).
-
December 11, 2009 at 4:44 pm #54216Theunis De JongMember
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
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”?
-
December 11, 2009 at 5:17 pm #54217Eugene TysonMember
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.
-
December 11, 2009 at 5:17 pm #54218Eugene TysonMember
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.
-
July 28, 2012 at 10:52 am #62699Erica GametParticipant
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!
-
-
AuthorPosts
- You must be logged in to reply to this topic.