Loading ...
Sorry, an error occurred while loading the content.

RegEx to trim white space from beginning and end of all lines

Expand Messages
  • joy8388608
    Just when I thought I was getting pretty good at RegEx, I see I still have a LONG way to go. I wanted to write a single find replace to trim tabs and spaces
    Message 1 of 7 , Nov 20, 2009
    • 0 Attachment
      Just when I thought I was getting pretty good at RegEx, I see I still have a LONG way to go.

      I wanted to write a single find\replace to trim tabs and spaces from the beginning and ends of all lines. Do NOT remove lines with just white space. They should just be changed to an empty line.

      It seems I could write a clip to do a ^$StrTrim$ on each line.

      It also seems to work with this clip...
      ; Remove white space at beginning of all lines
      ^!Replace "^[\t ]+(.*)" >> "$1" ATIWRS

      ; Remove white space at end of all lines
      ^!Select ALL
      ^!Menu Modify/Lines/Trim Blanks

      BUT, in order to learn something, I would like to see how a single replace would be coded.

      If anyone sees anything wrong with my BOL replace, please tell me.

      Thanks again for the help of everyone.

      Joy

      Here is my test file. Because you cannot see white space, \R is an empty line, \t is a tab and \s is a space.

      Line 1
      \R
      \R
      \R
      \t\tLine 2
      \s\t\s
      \s\tLine 3\s\t
      3837492
      \R
      \R
      \R
      Line 4
      \s848484
      \s
      \R
      \s\s\sLine 5
      \t\s\t
      Line 6
      \s\t
      \s\t\s\t\s\t\tLine 7
      \s\s\s\s\s\t
      \t\t\s
      \t
      Line 8\t\t
      \s\s\s
      Line 9
      \R
      \s\s\s
      \t\t
    • Don - HtmlFixIt.com
      ^!Replace ^[ t ]*(.*?)[ t ]*$ $1 ATIWRS Line 1 Line 2 Line 3 3837492 Line 4 848484 Line 5 Line 6 Line 7 Line 8 Line 9
      Message 2 of 7 , Nov 20, 2009
      • 0 Attachment
        ^!Replace "^[\t ]*(.*?)[\t ]*$" >> "$1" ATIWRS

        Line 1



        Line 2

        Line 3
        3837492



        Line 4
        848484


        Line 5

        Line 6

        Line 7



        Line 8

        Line 9




        joy8388608 wrote:
        > Just when I thought I was getting pretty good at RegEx, I see I still have a LONG way to go.

        > It also seems to work with this clip...
        > ; Remove white space at beginning of all lines
        > ^!Replace "^[\t ]+(.*)" >> "$1" ATIWRS
        >

        > Here is my test file. Because you cannot see white space, \R is an empty line, \t is a tab and \s is a space.
        >
        > Line 1
        > \R
        > \R
        > \R
        > \t\tLine 2
        > \s\t\s
        > \s\tLine 3\s\t
        > 3837492
        > \R
        > \R
        > \R
        > Line 4
        > \s848484
        > \s
        > \R
        > \s\s\sLine 5
        > \t\s\t
        > Line 6
        > \s\t
        > \s\t\s\t\s\t\tLine 7
        > \s\s\s\s\s\t
        > \t\t\s
        > \t
        > Line 8\t\t
        > \s\s\s
        > Line 9
        > \R
        > \s\s\s
        > \t\t
      • flo.gehrke
        ... That works fine, Don! Also this works for me -- without capturing anything... ^!Replace ^(?:([ t x20]+))|(?:(?1)$) AWRS Tested with NT 6.12 Pro.
        Message 3 of 7 , Nov 20, 2009
        • 0 Attachment
          --- In ntb-clips@yahoogroups.com, "Don - HtmlFixIt.com" <don@...> wrote:
          >
          > ^!Replace "^[\t ]*(.*?)[\t ]*$" >> "$1" ATIWRS

          That works fine, Don!

          Also this works for me -- without capturing anything...

          ^!Replace "^(?:([\t\x20]+))|(?:(?1)$)" >> "" AWRS

          Tested with NT 6.12 Pro.

          Regards,
          Flo
        • Don - HtmlFixIt.com
          Nice point Flo, more efficient indeed. ^ = at start (?: ) means group but don t capture?? t is tab x20 is spaces + means more than one ... the (?1) repeats
          Message 4 of 7 , Nov 20, 2009
          • 0 Attachment
            Nice point Flo, more efficient indeed.
            ^ = at start
            (?: ) means group but don't capture??
            \t is tab
            \x20 is spaces
            + means more than one
            | means or
            the (?1) repeats the subpattern?
            and the $ means at the end.

            About right?

            flo.gehrke wrote:
            > --- In ntb-clips@yahoogroups.com, "Don - HtmlFixIt.com" <don@...> wrote:
            >> ^!Replace "^[\t ]*(.*?)[\t ]*$" >> "$1" ATIWRS
            >
            > That works fine, Don!
            >
            > Also this works for me -- without capturing anything...
            >
            > ^!Replace "^(?:([\t\x20]+))|(?:(?1)$)" >> "" AWRS
            >
            > Tested with NT 6.12 Pro.
            >
            > Regards,
            > Flo
            >
            >
            >
            >
            > ------------------------------------
            >
            > Fookes Software: http://www.fookes.com/
            > NoteTab website: http://www.notetab.com/
            > NoteTab Discussion Lists: http://www.notetab.com/groups.php
            >
            > ***
            > Yahoo! Groups Links
            >
            >
            >
            >
          • hsavage
            ... still have a LONG way to go. ... from the beginning and ends of all lines. Do NOT remove lines with just white space. They should just be changed to an
            Message 5 of 7 , Nov 20, 2009
            • 0 Attachment
              joy8388608 wrote:
              > Just when I thought I was getting pretty good at RegEx, I see I
              still have a LONG way to go.
              >
              > I wanted to write a single find\replace to trim tabs and spaces
              from the beginning and ends of all lines. Do NOT remove lines with
              just white space. They should just be changed to an empty line.
              >
              > It seems I could write a clip to do a ^$StrTrim$ on each line.
              >
              > It also seems to work with this clip...
              > ; Remove white space at beginning of all lines
              > ^!Replace "^[\t ]+(.*)" >> "$1" ATIWRS
              >
              > ; Remove white space at end of all lines
              > ^!Select ALL
              > ^!Menu Modify/Lines/Trim Blanks
              >
              > BUT, in order to learn something, I would like to see how a single
              replace would be coded.
              >
              > If anyone sees anything wrong with my BOL replace, please tell me.
              >
              > Thanks again for the help of everyone.
              >
              > Joy

              Joy,

              Regex is a powerful tool can save a lot of work but, if I understand
              correctly, what you need is already built-in to NoteTab;

              H="Remove Whitespace"
              ^!Select ALL
              ^!Menu Modify/Alignment/Left Align
              ^!Menu Modify/Lines/Trim Blanks

              ·············································
              ºvº SL_day# 324 - created 2009.11.20_19.57.24

              Suburbia; where they tear out the trees -
              • then name streets after them.

              € hrs € hsavage € pobox € com
            • flo.gehrke
              ... Agreed, Don! With one small correction: + doesn t mean more than one but one or more , equivalent to {1,}. Regards, Flo
              Message 6 of 7 , Nov 20, 2009
              • 0 Attachment
                --- In ntb-clips@yahoogroups.com, "Don - HtmlFixIt.com" <don@...> wrote:
                >
                > Nice point Flo, more efficient indeed.
                > ^ = at start
                > (?: ) means group but don't capture??
                > \t is tab
                > \x20 is spaces
                > + means more than one
                > | means or
                > the (?1) repeats the subpattern?
                > and the $ means at the end.
                >
                > About right?

                Agreed, Don! With one small correction: + doesn't mean "more than one" but "one or more", equivalent to {1,}.

                Regards,
                Flo
              • joy8388608
                ... Thank you all so very much for the multiple answers. I learned something from each of them. And this last reply is the best and what I was looking for. I
                Message 7 of 7 , Nov 20, 2009
                • 0 Attachment
                  --- In ntb-clips@yahoogroups.com, hsavage <hsavage@...> wrote:

                  > Regex is a powerful tool can save a lot of work but, if I understand
                  > correctly, what you need is already built-in to NoteTab;
                  >
                  > H="Remove Whitespace"
                  > ^!Select ALL
                  > ^!Menu Modify/Alignment/Left Align
                  > ^!Menu Modify/Lines/Trim Blanks

                  Thank you all so very much for the multiple answers. I learned something from each of them. And this last reply is the best and what I was looking for. I never had the need to left align and never truly noticed it was there. I also wouldn't have realized it would slide everything to the left to get rid of all white space.

                  Thanks again.

                  Joy
                Your message has been successfully submitted and would be delivered to recipients shortly.