14 Replies Latest reply on Nov 18, 2011 5:51 PM by TroyCole

    GREP Search to apply superscript


      Hi there,

      I'm a longtime InDesign user but I'm pretty new to GREP.


      Currently I'm working on a French document, but this could easily apply to English documents as well.


      I'm trying to use GREP to identify and apply superscript to letters that come after numerals indicating their position (i.e., 1st, 2nd, 3rd, etc.)


      The code that I've written that I "hoped" would do this is:




      What I hoped this query would do is:


      1 - positive look behind for a white space followed by one or more numbers (?<=\s\d+)

      2 - find any one or more letter characters [\l\u]+

      3 - positive look ahead for a white space


      A sample bit of text that I'm trying this on includes the following text:


      ...dans le cadre du 40e anniversaire...


      My hope was that this would select the "e" which I could then apply superscript to. I can get parts of the code to work individually but I can't get the full query to work.


      If anyone can point me in the right direction to get this working I would really appreciate it.


      Thanks in advance!


        • 1. Re: GREP Search to apply superscript
          SJRiegel Adobe Community Professional & MVP

          This seems to work for me.




          I just changed the positive look behind to find just one number.

          Does it change anything you don't want changed when it's phrased this way?

          • 2. Re: GREP Search to apply superscript
            TroyCole Level 1




            I've just run it and realized that the modified query would change a letter in a postal code (I'm in Canada) to superscript. For example the "J" in the following postal code would become superscript - B3J 2S9. Is there a way to "exclude" the postal code scenario?




            Hi SRiegel,

            Thanks for the quick reply!


            Searching for a single digit does work for me as well so perhaps that's fine. I can't think of any situations where this would change something that I don't want changed. And in fact I'm now realizing that searching for the space before the numerals would mean that if I ever have text with a paragraph starting with one of these numbers followed by the positioning letters, that it would not be recongnized.


            Just out of curiosity, do you have any thoughts on why simply adding a "+" to your modified query to look for one or more numerals (?<=\d+) causes it to fail? Shouldn't "40" fall into the one or more category? I don't think the "+" actually adds anything to the query, but at this point I'm just trying to understand GREP better, and I don't really understand why this does not find anything?


            Any thoughts?




            Message was edited by: TroyCole

            • 3. Re: GREP Search to apply superscript
              Peter Spier Most Valuable Participant (Moderator)

              I'm not absolutely certain about this, but I think the problem is that a lookbehind cannot be of variable length.

              • 4. Re: GREP Search to apply superscript
                SJRiegel Adobe Community Professional & MVP

                You can't use + in a lookbehind. It only accepts fixed-length expressions.

                You could look for, say, exactly 3 digits,




                but not 'one or more' or 'zero or more", etc.


                that's just how the language works.

                • 5. Re: GREP Search to apply superscript
                  [Jongware] Most Valuable Participant

                  Remind me again, what letters get superscripted after a number in French?


                  You can use something like this:




                  which will pick up "4e" and "40e" -- add more digits in the first OR group, more (single) characters can be added in a character set [like this] instead of just 'e'.


                  WhatTheGrep's breakdown:


                  (  Begin Group #1

                            (?<=  Lookbehind Group

                                      \b  Word boundary

                                      \d  Any digit (0..9)

                            )  End Lookbehind Group

                            |  Match previous part OR next part inside Group #1

                            (?<=  Lookbehind Group

                                      \b  Word boundary

                                      \d  Any digit (0..9)

                                      \d  Any digit (0..9)

                            )  End Lookbehind Group

                  )  End Group #1

                  e  Literal character “e”

                  \b  Word boundary



                  (1(?<=<\b \d <) |1(?<=<\b \d \d <) 1)e \b

                  • 6. Re: GREP Search to apply superscript
                    Mary Posner Level 3

                    Does this work for you?




                    Seems to work without messing up a Canadian zip code. (BTW, if it looks familiar, this is a tweak on some code you posted in InDesign Secrets awhile back!)

                    • 7. Re: GREP Search to apply superscript
                      TroyCole Level 1

                      OK... that's embarassing!


                      Honest, I've done other things since 2009 but obsess over using GREP to apply superscript! I know that I messed around with this a bit in the past, but I totally forgot that I had been working on the superscript thing, let alone that I put out call for help!


                      Now that I'm revisiting GREP a bit I'm trying to create some queries that I can save for common searches and I'm also just discovering the power of GREP styles for common things like eliminating orphans, applying en dashes between numerals, etc. The code that I had put out there previously for the superscript does seem to work. I guess this time around I was trying to make something a little more robust that would find the instances by identifying the sequence rather having to write in an "or" statement identifying all possible suffixes. But honestly it does work and if I find more suffixes, I'll just add them to the or statement.


                      Some previous replies have identified what I think my main problem was with the code earlier today. I was trying to search for \d+, but the + part does not work in a positive look behind. So it was stopping the search in it's tracks.


                      I realize that I'm just scratching the surface, but I'm looking forward to getting more comfortable with GREP in general.


                      Thank you all for the help!


                      • 8. Re: GREP Search to apply superscript
                        TroyCole Level 1

                        Hi Jongware,

                        I guess it must have worked for you, otherwise you wouldn't have posted it. But when I tried running your code but it didn't seem to work for me. It just says "Cannot find match."


                        Not sure if I'm missing something?



                        • 9. Re: GREP Search to apply superscript
                          Mary Posner Level 3

                          Nothing to be embarrassed about -- or at least if you are embarrassed, you're not alone! I just did the same thing with a script I'm working on, hunted around for some relevant code, only to find it in a script I'd completely forgotten I'd worked on at the beginning of this year.


                          I keep trying to chime in on these GREP discussions because I can see how powerful it is for so many things, and I'm trying to learn it too. It also has the potential to do a lot of damage if you don't know the pitfalls, especially if you're doing a document-wide search-and-replace.


                          Anyway, give that a try and let me know if that works for you. I'm curious to see if there are any other false matches that the above code didn't take into account.



                          P.S. If you haven't looked at it yet, check out Michael Murphy's excellent PDF breaking down the elements of a GREP search:



                          • 10. Re: GREP Search to apply superscript
                            [Jongware] Most Valuable Participant

                            Mary, those color-coded explanation might very well have been my inspiration for WhatTheGrep!


                            (Which is a handy script to write out a diagram of - almost - every GREP: http://www.jongware.com/idgrephelp.html )

                            • 11. Re: GREP Search to apply superscript
                              Mary Posner Level 3

                              Nice, Jongware, I'll have to give that a try!


                              When I'm testing a GREP code, I sometimes do it by creating a GREP style that will apply a really obvious character style (like bold and red) to any found text. That may not help me much if I'm trying to do a bunch of subexpressions to swap around or delete text in the replacement string, but for relatively simple searches, it works well, and shows me where the code needs to be made non-greedy. (I think that tip came from Michael Murphy too!) If you're not starting with raw, unstyled text, you can copy your text into another document, apply your GREP style to all, and experiment away. But I'll definitely have to give your script a try -- thanks for posting the link!



                              • 12. Re: GREP Search to apply superscript
                                TroyCole Level 1

                                Hi Jongware,

                                I see now that your original post was showing results from WhatTheGrep!. Very cool! I've just downloaded and installed it. It seems like it will be very handy for someone like myself who is new to GREP.


                                Although I'm running it in CS5, and all of the text that should be showing up in black, shows up in white. Has anyone else reported this to you?



                                • 13. Re: GREP Search to apply superscript
                                  [Jongware] Most Valuable Participant

                                  What the Question Mark!?


                                  It seems [Paper] and [Black] switched positions in the Swatches panel between CS4 and CS5, resulting in the totally unlogical order "None", "Registration", "Paper", "Black". What is this, one of Adobe's Expert Forum Driven Improvements!?




                                  Okay, I updated the zip file -- it ought to work on both CS4 and CS5, theoretically that is.

                                  • 14. Re: GREP Search to apply superscript
                                    TroyCole Level 1

                                    Wow! Thanks for the quick update. I just downloaded the new version and gave it a try... works like a charm now!


                                    Thanks for the time you put into this!