5 Replies Latest reply on Mar 12, 2012 6:43 AM by Peter Spier

    GREP remove white space at start and end of paragraph

    TᴀW Adobe Community Professional & MVP

      Hi,

       

      Question for the GREP experts: I'd like to construct a GREP expression

      that will remove all white space at the start AND end of a paragraph in

      one go.

       

      What I've got so far works, but you have to run it twice to work:

       

      I'll try and post it at the bottom of this message, but since I'm

      posting via email it probably will come out scrambled, so I'll describe

      what I've got so far:

       

      Two expressions in parenthesis separated by the OR character (pipe). The

      first expression searches for one or more space, tab or white-space at

      the beginning of a paragraph.

       

      The second expression searches for the same at the end of the paragraph.

       

      Each expression is in parenthesis, and the two groups are separated, as

      I say, by an OR sign.

       

      However, this has to be run twice for it to work. For it removes all the

      spaces at the start of paragraphs, then when run again all the spaces at

      the end.

       

      Any ideas how to get it to work in one go?

       

      Thanks,

      Ariel

       

      The expression is:

      (^[ \ts]+)|([ \ts)+$)

       

        • 1. Re: GREP remove white space at start and end of paragraph
          Peter Spier Most Valuable Participant (Moderator)

          I'm hardly one of the experts, but I'd try ^\s*(.+)\s*$ and replace with $1

          • 2. Re: GREP remove white space at start and end of paragraph
            TᴀW Adobe Community Professional & MVP

            Thanks Peter.

             

            Your suggestion should work. I was trying to combine two greps that I

            have (one for the beginning and one for the end) into one, so I was

            looking at this from the wrong angle.

             

            Thanks,

            Ariel

            • 3. Re: GREP remove white space at start and end of paragraph
              [Jongware] Most Valuable Participant

              Arïel, your GREP appears a bit mashed up in the forum -- was it something like this?

               

              (^[\t\s]+)|([\t\s]+$)

               

              If so:

               

              1. "\s" is "any whitespace", so you would not need to include "\t" as well. But I would not recommend this, as a HUGE disadvantage of "\s" is it also includes hard and soft returns: double returns are seen as "any whitespace" (the first return) and then "end of line" (the second return), and the first is removed. ("Disadvantage" is subject to what you want to accomplish, by the way. I usually don't want this so I list each possible whitespace character that may be removed.)

               

              2. The parentheses around each 'section' of this GREP are not necessary. The OR operator is 'highest level' -- in this case, since you have an entire left part OR an entire right part, you can omit all parentheses.

               

              3. This works for me ...

               

              ^[\t ]+|[\t ]+$

               

              ... and despite Peter's not-too-shabby suggestion, I would still recommend using mine instead. It's easier on ID (since it only has to remove text, not "remove-and-then-insert-again" (which is exactly what using the $.. operators do), and, more importantly, there are limits to what $1 can 'pick up'. I can't recall the special circumstances, but I have had text attributes shift, and footnotes and anchored objects disappear/being mucked up/otherwise mangled.

              • 4. Re: GREP remove white space at start and end of paragraph
                TᴀW Adobe Community Professional & MVP

                Jongware,

                 

                It was, just that I threw in a space for good measure. (^[ \t\s]+) etc. for no apparent reason.

                 

                1. I see that you're right about \s grabbing double paragraphs. Usually I avoid these in books, so it hasn't been a problem.

                2. I'm always wary of relying on operator precedence -- it can be hard to remember and debug, so I just throw in some parenthesis to be on the safe side.

                3. It works, but it also has to be run twice to catch everything. I was wondering if there is an expression that will catch everything in a single go.

                 

                Peter, I've been playing around with your expression, but apart from Jongware's caveats, it's not working for me for a paragraph that ends in space tab space tab, not sure why. Also, if there are no characters in a paragraph, just an empty paragraph with spaces and tabs (as authors like to add betimes for reasons closely guarded by the International Conspiracy of Authors Inc.) it won't get rid of them because it has to find some characters.

                 

                Thanks for your help and I'll be interested to know if you have an answer.  By the way, I'm using the web forums right now, and there is no sign of those stars to award points for correct and helpful answers otherwise I would click them.

                 

                Ariel

                • 5. Re: GREP remove white space at start and end of paragraph
                  Peter Spier Most Valuable Participant (Moderator)

                  Arïel wrote:

                   

                   

                  Peter, I've been playing around with your expression, but apart from Jongware's caveats, it's not working for me for a paragraph that ends in space tab space tab, not sure why. Also, if there are no characters in a paragraph, just an empty paragraph with spaces and tabs (as authors like to add betimes for reasons closely guarded by the International Conspiracy of Authors Inc.) it won't get rid of them because it has to find some characters.

                   

                  First, Jongware is much brighter than I am about this stuff, but I guess I consider removal of empty paragraphs a bonus.

                   

                  I found the same problem with the trailing space after looking (hadn't tested the query), in fact it even left just a single space at the end. I think changing to ^\s*(.+?)\s*$ solves that. The (.+), being greedy, was grabbing the trailing whitespace, too. I'm not seening the problem with a paragraph containing only whitespace, however, in either version.