9 Replies Latest reply on Dec 2, 2012 9:14 AM by sperry1975

    GREP Question

    sperry1975 Level 1

      Could someone tell me how to write a GREP code that works something like this:

      [\l\u]+~_\w+

      Where the line it locates would look like this:

      wordortext—wordortext

      The GREP code above does the job but the thing is I only want the em dash highlighted so I can do a change on only the em dash. Some might say why not do a search for ~_ only. The reason is it's a large document with other em dashes that are arranged in different ways that I don't want effected. The GREP I provided above locates only what I want, but highlights the words in fron and after the em dash which wont work if I intend to change only the em dash to an en dash with \s before and after. Can someone please help?

        • 1. Re: GREP Question
          Peter Spier Most Valuable Participant (Moderator)

          What you want are lookahead and lookbehind.

           

          The question I have is do you have cases of an em dash with only a single character on either side that you DON'T want to match? (?<=\w)~_(?=\w) should match any emedash that has a word character on both sides.

          • 2. Re: GREP Question
            sperry1975 Level 1

            Thanks Peter! I'm very curious how you came up with that GREP. It does the job partly, but as you've pointed out I did locate instances where there are em dashes with words on either side I don't want to match. In fact it's something else I need to change. Here are the two instances:

            Chapter— #(s) Title

            Section— #(s) Title

            If you could tell me how you managed to search for the word directly before and after the emdash without including the emdash I would like to try to figure out how to develop a line of code to locate the two instances above without highlighting the words Chapter— #(s) but still highlighting the space before Title so I can place a ^p in front of it. So don't tell me everything just yet, just let me know how you did that code you did before and give me a chance to figure out the rest, please!

            • 3. Re: GREP Question
              Peter Spier Most Valuable Participant (Moderator)

              as I said, it's a look-behind: (?<= put what you want to look for here), and a look-ahead: (?= put what you want to look for here). The search only matches what is in front of a look-behind of behind a look-ahead, so in this case it matches only the em dash. I'm hardly an expert in the way this is parsed, but I think what happens is ID finds the em dash, then checks to see if it has the required character(s) from any look-ahead or look-behind. If those criteria ar not met, then ther is no mathch and the search continues. There are negative versions of the lookarounds too, so you can match only when not surroounded by something in particular, and all of them can be used singly or in any combination in your query. You just have to put them in the correct positions -- lookbehinds come before the pattern you want to match, and lookaheads come after -- in your expression.

               

              I have a handy reference chart I downloaded from Mike Witherell who used to be a responder here. he's got a lot of useful stuff at http://jetsetcom.net/useful-resources/indesign-cs6-resources.html

               

              I also highly recommend Peter Kahrel's book on GREP in InDesign that you can get in a variety of electronic formats for about $10 from O'Reilly.

              • 4. Re: GREP Question
                sperry1975 Level 1

                Excellent! That's what I couldn't figure out because I didn't have an understanding of the use of the look-behinds/aheads. Thanks for the resources by the way. So the way to go for the two instances I mentioned above is to

                Find What:

                (?<=Chapter \d)~_

                or

                (?<=Section \d)~_

                and Change to:

                ~_\r

                If you have any other suggestions that would be great, but this seems to be working. Thanks a lot for the help!

                • 5. Re: GREP Question
                  Peter Spier Most Valuable Participant (Moderator)

                  In the case where you just want to add something to the found text you can use $0 in the change field for the found text -- $1 - $10 are used for found subexpressions that are contianed within parentheses, which I think you already know. It has the same effect as typing the tring again, but can be a lot faster for a long string and is of course flexible in the case where the found string might be variable in its content.

                   

                  In this case that would translate from your ~_\r to $0\r which is pretty trivial, but if your search was returning a variety of characters it would be essential.

                   

                  It's also important to note that your look-behind will not find two-digit or longer chapter numbers. I'm not sure that it is possible to write a look around expression that will find both single and multiple digit numbers as they are not terribly flexible in structure. maybe Jongware or one of the other real GREP experts will let us know.

                  • 6. Re: GREP Question
                    sperry1975 Level 1

                    Thanks for the tips Peter.

                    I'm now trying to figure out how to write a GREP that looks for a single line paragraph.

                     

                    Example:

                         It's also important to note that your look-behind will not find two-digit or longer chapter numbers. I'm not sure that it is possible to write a look around expression that will find both single and multiple digit numbers as they are not terribly flexible in structure. maybe Jongware or one of the other real GREP experts will let us know.

                         <Single line paragraph here.>

                         It's also important to note that your look-behind will not find two-digit or longer chapter numbers. I'm not sure that it is possible to write a look around expression that will find both single and multiple digit numbers as they are not terribly flexible in structure. maybe Jongware or one of the other real GREP experts will let us know.

                    • 7. Re: GREP Question
                      Peter Spier Most Valuable Participant (Moderator)

                      I don't think that's doable with GREP. As far as I know it has no concept of lines. I'm pretty sure it can be done through scripting, however.

                      • 8. Re: GREP Question
                        [Jongware] Most Valuable Participant

                        It's kind of doable but only if you can put a number to the length of "a short line".

                         

                        Try this:

                         

                        ^.{1,20}$

                         

                        and you'll see it will happily locate any paragraph ranging from 1 to 20 characters long -- which definitely sounds as "a short line" to me, but it actually depends on your column width, font size, spacing etc. whether this straddles one or two *lines*.

                        • 9. Re: GREP Question
                          sperry1975 Level 1

                          Thanks a lot. That worked great to help me locate those single lines.