1

I am trying to find a regex to use in InDesign that could select every nth paragraph in a text box (nth as in random, not as in sequence).

In the following example for instance, I would like to be able to select the 2nd, the 3rd and the 5th paragraph by inputing 2,3 and 5 somewhere in a regex.

enter image description here

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
gmorissette
  • 291
  • 1
  • 3
  • 13

2 Answers2

3

This needs to be done as a script. See below for an example to get you started. The script assumes that a textframe containing your paragraphs is selected when you run the script! Note: there is no effort to check/handle errors (e.g. giving a non numeric input for paragraph numbers). You'll need to add this yourself. You could modify the input to accept a comma delimited list of paragraph numbers if needed as well.

var doc = app.activeDocument;
var frame = app.selection[0];

var para = parseInt(prompt("Paragraph:", ''));    

//replace TestStyle with your desired style name
var style = app.activeDocument.paragraphStyles.item('TestStyle');

frame.parentStory.paragraphs[para - 1].appliedParagraphStyle = style;
Anna Forrest
  • 1,711
  • 14
  • 21
  • I think there's something I'm missing here. I run the script, the prompt appears, but then I can only manage to apply a style to a single paragraph in the text box. Let's say I've entered 1,2 only the first paragraph will have the style applied to it.. Everything seems flawless otherwise – gmorissette Sep 04 '14 at 21:53
  • The above code is only a sample. You need to modify it to suit your needs – Anna Forrest Sep 04 '14 at 22:46
  • I am not fluent at all in JS.. Any advice? – gmorissette Sep 04 '14 at 22:50
  • Refer to this question on how to split a string in javascript. http://stackoverflow.com/questions/96428/how-do-i-split-a-string-breaking-at-a-particular-character. Then you'll need to loop through all the values in the array, applying the paragraph style to each value - 1 paragraph. – Anna Forrest Sep 05 '14 at 00:21
  • Hmm.. Wish I could understand. I'm so sorry. Perhaps you could simply edit your answer (the JS code itself) I would love to mark this as accepted and boost you rep. – gmorissette Sep 05 '14 at 00:39
  • 1
    Because then you won't learn anything and will just wind up back here next time you have a question. Better you have a go and edit your question with your most recent attempts. Then I can give you more pointers on what you are still missing. – Anna Forrest Sep 05 '14 at 01:57
  • 4 initial lines + following code = success with paragraph numbers marked as CSVs //// for (var i = 0 ; i < paras.length ; i++) { frame.parentStory.paragraphs[parseInt(paras[i]) - 1].appliedParagraphStyle = style; } – gmorissette Sep 14 '14 at 22:12
-1
/([^\n]+\n)/g

then use grouping to extract the paragraphs you desire.

Jason Hu
  • 6,239
  • 1
  • 20
  • 41
  • Where am I supposed to enter my paragraph numbers exactly? – gmorissette Aug 23 '14 at 02:53
  • @luimeme you make it a function, and your paragraph number is used in grouping, but in the regex. – Jason Hu Aug 23 '14 at 02:53
  • What do you mean by grouping exactly? I am not familiar at all w/ regex – gmorissette Aug 23 '14 at 02:57
  • @luimeme depend on your language, global matching should return a list, in which all elements should match the regex. in perl, it goes `my @matched = m/([^\n]+\n)/g;` then just `$match[$paras[0]]` on and on to extract your target. – Jason Hu Aug 23 '14 at 03:00
  • Hmm ok in InDesign you can only enter a single line of code in the GREP dialogue box.. No multiple operations. Any way to achieve what I would like to do at once? – gmorissette Aug 23 '14 at 03:06
  • 1
    @luimeme if your software does not allow extra code, it's impossible for regex to achieve you goal. – Jason Hu Aug 23 '14 at 03:32