9 Replies Latest reply on Jul 16, 2011 5:30 PM by Peter Spier

    GREP: find all spaces before punctuation, quotes problem…

    Rand0mUser Level 1

      Hello,

       

      I'm trying to figure how to search for all spaces (\s) before punctuation, except for quotes, brackets and parenthesis.

       

      Example

       

      Hello ! My name is Eric .

      “ Hello ” ! My name is Eric.

       

      Should be

       

      Hello! My name is Eric.

      “Hello”! My name is Eric.

       

      My amazing newbie skills came up with this: (?<=\l)\s+(?=[[:punct:]])

      I can't figure out how to get the whitespace between the first quotation mark and the H in Hello. I also don't want it to catch paragraph returns (which my GREP query managed to do). Maybe I need two separate queries?
      Please help!
        • 1. Re: GREP: find all spaces before punctuation, quotes problem…
          Peter Spier Most Valuable Participant (Moderator)

          Since none of the real GREP experts have stepped in overnight, I guess I should go a head and take a stab.

           

          First, I think you really mean except for before OPENING quotes, brackets and parentheses, and I'm not entirely sure you always want to eliminate space in front of underscores, slashes, some periods, asterisks or hyphens. This is dangersous ground and depending on what the subject matter might be there are lots of cases for odd punctuation to start a word -- Microsoft .Net comes to mind, or -10 -- so you need to think very carefully about if there are going to be cases where you don't want to remove the space. You might need to do several searches if there are, making a class of all the marks that always should be fixed, and another class for those that you should check one at a time.

           

          As for the other part of the question, I think, yes, you need to do two searches.

          • 2. Re: GREP: find all spaces before punctuation, quotes problem…
            Rand0mUser Level 1

            Ah, I need to put the situation into context.

             

            I work for a community newspaper and we receive all sorts of horrible text

            files. I bring them into  InDesign and use Kahrel's clean up script to do

            most of the work. Unfortunately, it doesn't catch the extra(or more) space

            that comes before punctuation marks like commas, periods, exclamation and

            question marks. It also doesn't catch spaces between (as you said) opening

            and closing quotes, brackets and parentheses. It's all about the space that

            comes before.

             

            You mentioned underscores, slashes, some periods, asterisks and hyphens. I

            think the query should also include them except for asterisks and hyphens.

             

             

            Regards,

            Eric

            • 3. Re: GREP: find all spaces before punctuation, quotes problem…
              Peter Spier Most Valuable Participant (Moderator)

              Well you're free to include anything in your Query that you wish. I'm just providing a little cautionary information.

               

              Do you know how to create a class for the punctuation marks you want to include?

               

              And yes, you are going to need a second query (as mentioned before) to catch the space after an open quote, bracket or parenthesis. Peter K haas a script that will allow you to chain them into a single operation.

              • 4. Re: GREP: find all spaces before punctuation, quotes problem…
                Rand0mUser Level 1

                Thanks for the help and encouragement!

                 

                Here's what I came up with:

                 

                1st query: \s+(?=\!|\?|\.|\,)

                 

                2nd query: (?<=\(|\[|\{|\“|\«|\‘)\s+

                 

                3rd query: (?<=\l)\s+(?=\)|\]|\}|\”|\»|\’)

                 

                 

                I put these queries in Peter K's Clean up spurious white space script, but it's not workin'

                 

                Would someone like to assist me for that?

                • 5. Re: GREP: find all spaces before punctuation, quotes problem…
                  Peter Spier Most Valuable Participant (Moderator)

                  I don't believe you can use an "or" in a look ahead/behind. Waht you want to do is look for the space followed by a class with the punctuation in it, something on the order of (\s+)([.,!:-?]) and replace with $2 I think.

                  • 6. Re: GREP: find all spaces before punctuation, quotes problem…
                    Eugene Tyson Adobe Community Professional & MVP

                    I've been at this for several hours. I cannot come up with a GREP expression to catch all.

                     

                    In my opinion you'll have to do it on a case by case basis.

                     

                    The biggest area where it failed was finding "Hello" , "How are you" , I'm good

                     

                    I can't write a GREP expression to surround all posibilities. After a few hours of tinkering, I figure it would be more prudent to sort this out with a case by case basis.

                    • 7. Re: GREP: find all spaces before punctuation, quotes problem…
                      marytro

                      I apologize for my ignorance but you want me to type in $2 where I think the period is?

                      • 8. Re: GREP: find all spaces before punctuation, quotes problem…
                        marytro Level 1

                        Ok, not sure what a GREP is but how do I correct the problem on a case by case basis.....sorry I don't understand what I should do!

                        • 9. Re: GREP: find all spaces before punctuation, quotes problem…
                          Peter Spier Most Valuable Participant (Moderator)

                          GREP is what you are using, the search for patterns using "regular expressions." The $2 goe inthe replace field if you are doing the sort of search I recommended. Putting parentheses around parts of the search terms divieds them into chunks, and in the replace field you refer to the found text in each chunk by using $ followed by the number that corresponds to the group in the order they are read, so $2 in the above example is the punctuation that was found. By replacing the whole string -- white space plus punctuation -- with only the punctuation you are deleting the white space.

                           

                          You really shold invest a few bucks in Peter Kahrel's GREP primer. You'll find it very helpful.