[Disclaimer] 04/02/12. — As ironically noted by JRB (in French), tweaking GREP to loop character styles is fun but utterly useless since nested styles achieve the same result much more simply! (See Comments section.) Nevertheless, I decided to leave this article online for posterity!

Say you have a set of various InDesign character styles that you want to apply to your text on a cyclic basis. A typical goal is to get alternating character colors, anyway the trick below can be used for any formatting provided that you can control the corresponding attributes through character styles.

Well, how to automate such style cycle? Of course a script could easily do the job—as illustrated in this InDesignSecrets Forum's post—but then we don't get dynamic update so we are forced to restart the script whenever the text changes.

Another approach is to use GREP styles as follows:

GREP Style Settings.

The trick is based on the fact that GREP style rules are sequentially applied from top to bottom, which means that any text pattern already affected by a rule can still be re-affected by the following rule(s). Then we use positive lookbehinds to create somewhat character offsets, in the following order:

Style2
(?<=.).{1,4}
 
Style3
(?<=..).{1,3}
 
Style4
(?<=...).{1,2}
 
Style5
(?<=....).
 

Voilà. Enjoy!