7 Replies Latest reply on Aug 21, 2015 2:14 AM by Peter Spier

    Ungreedy grep

    Martijn Busink

      I'm trying to write a GREP to change dashes to em-dashes and have non-breaking spaces around the text between them. I managed to write this in http://regex101.com (see below). I have some trouble translating it to Indesign. It's the U behind the slash to make it Ungreedy (otherwise it starts at the first dash and ends at the last dash in the paragraph. The opening and closing /s don't work like that in Indesign.


      Schermafbeelding 2015-08-18 om 11.43.18.png

        • 1. Re: Ungreedy grep
          Peter Spier Most Valuable Participant (Moderator)

          Since you have told us what you mean by the original dash (Hyphen? En Dash? Em Dash) you make it more complicated, but this should find any of them, I think:



          Find ([-~=~_]\s)(.+?)(\s[-~=~_])

          Change ~_~S$2~S~_


          The ? makes it non-greedy.


          Peter Kahrel's $10 eBook on GREP in InDesign is a gem: GREP in InDesign - O'Reilly Media

          • 2. Re: Ungreedy grep
            Martijn Busink Level 1

            Well, I guess the 'dash' should've been 'hyphen', but that wasn't my problem.


            Thanks a bunch, that question mark was the answer to my question!

            • 3. Re: Ungreedy grep
              Obi-wan Kenobi Level 5



              It won't work in 2 cases: footnotes and if only one dash, the text part finishing by a dot. 

              • 4. Re: Ungreedy grep
                Peter Spier Most Valuable Participant (Moderator)

                Two dashes was part of the spec, so a failure when there is only one is expected. Whether footnotes would be a problem would depend on the content of the footnotes, but why should it fail in footnotes?

                • 5. Re: Ungreedy grep
                  Obi-wan Kenobi Level 5

                  Not "in" but because of the "note call" in a current para!  Here's a good sample:


                  Capture d’écran 2015-08-21 à 01.04.04.png

                  • 6. Re: Ungreedy grep
                    Martijn Busink Level 1

                    The reason I wanted this was to have non-breaking spaces before and after the text that's surrounded by the en dashes and change the hyphens into en-dashes (because not many Word users type en dashes in such a gase). The hyphen has to be surrounded by spaces (in Dutch this is now common) because otherwise it would get actual hyphens too, people do use spaces around the hyphens if used as "gedachtenstreepjes" (the alternative for parentheses) so that's somehting that can be tackled.


                    In English text I'd use em dashes without spaces, something many people don't really like (even Americans do I heard from a friend) but I don't think spelling is about taste.


                    Oh and sorry for the miscommunication about the - (hypen), – (en dash/~=) and — (em dash/~_), always hard to get technical in English.


                    Totally getting that book on Grep you recommend Peter, thanks for the heads up.

                    • 7. Re: Ungreedy grep
                      Peter Spier Most Valuable Participant (Moderator)

                      If you might have footnote markers in the text between the dashes, this modified query seems to work: ([-~=~_]\s)(~F|.+?)(\s[-~=~_]), but the footnote marker thing is apparently a bug (according to Peter Kahrel).