• Login
Community
  • Login

Is there a possible way to REGEX "Trim Leading Space"?

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
9 Posts 4 Posters 9.0k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C
    Chris Tanguay 0
    last edited by Aug 19, 2021, 9:10 PM

    Question. Frequently, after doing several sets of FIND/REPLACES (typically on 7-12 open files at once), I’ll end up with these random leading spaces left throughout the files. For these particular type of text files (.srt subtitle files), there can be NO leading spaces. Everything has to be aligned to the left. Most of the time, they’ll be in the form of leading spaces, but also could be leading ‘tabbed’ spaces.
    So, Notepad++ has the “EDIT —> BLANK OPERATIONS --> TRIM LEADING SPACE” function, which takes care of all those pesky leading spaces & tabbed spaces. But, even as a MACRO, I have to run that function one file at a time, and it’s slower than I’d like.
    I was hoping there might be a way to REGEX this, so I can use the FIND/REPLACE along with the “REPLACE ALL IN ALL OPENED DOCUMENTS”.
    If there is a way, maybe, to do this with REGEX?
    If yes, could you also show me what the TRIM TRAILING SPACE regex syntax would be?
    Thanks in advance! - Chris

    P 2 Replies Last reply Aug 19, 2021, 9:12 PM Reply Quote 0
    • P
      PeterJones @Chris Tanguay 0
      last edited by Aug 19, 2021, 9:12 PM

      @Chris-Tanguay-0 ,

      Regular expression mode, FIND ^\h+ (beginning of line, followed by 1 or more horizontal spaces (basically, spaces and tabs) and REPLACE with empty

      1 Reply Last reply Reply Quote 0
      • P
        PeterJones @Chris Tanguay 0
        last edited by Aug 19, 2021, 9:12 PM

        @Chris-Tanguay-0 said in Is there a possible way to REGEX "Trim Leading Space"?:

        TRIM TRAILING SPACE regex syntax would be?

        Sorry, didn’t see that. FIND = \h+$ (one or more spaces, followed by end of line)

        P 1 Reply Last reply Aug 19, 2021, 9:14 PM Reply Quote 0
        • P
          PeterJones @PeterJones
          last edited by PeterJones Aug 19, 2021, 9:15 PM Aug 19, 2021, 9:14 PM

          @Chris-Tanguay-0 ,

          At this point, you’ve asked enough regex questions in the forum that really, you should be reading the official regular expression documentation in the online user manual at https://npp-user-manual.org/docs/searching/#regular-expressions , and trying to figure these out for yourself. The trim-leading and trim-trailing are not overly complicated expressions, and the user manual covers the meaning of each individual piece.

          C 1 Reply Last reply Aug 19, 2021, 9:40 PM Reply Quote 0
          • C
            Chris Tanguay 0 @PeterJones
            last edited by Aug 19, 2021, 9:40 PM

            @PeterJones First, thanks. And second, understood. Trust me, I’ve been at that document daily. But, it’s a bit of a challenge for me to find my way (and sync) with the syntax. I’ve figured out several things myself, but others… not so much.


            Allow me to at least mention another example (and good gravy, DO NOT give me the answer - especially if it’s an easy REGEX, and I’m sure it is), but hear me out.
            So I need to come up with a regex, to run on multiple open text files, where each of the text files will have a random # of empty (no printable text) lines at the end (CR LF). Could be anywhere from 2 lines, up to 50.
            I need to remove all of those extra blank lines, EXCEPT for 1. After the last character of printable text, I need just 1 “CR LF” sequence and that’s it. See photos. Now, to me, that already sounds complicated.
            So, I will go to the document, and try to figure this out myself. I do wish it was a bit more interactive, so I could maybe pop in a search term to get myself closer to the good stuff. Anyway, heading there now. Of course, if this one is tricky, help a guy out!
            from this:
            01.jpg
            to this:
            02.jpg

            P 1 Reply Last reply Aug 19, 2021, 9:46 PM Reply Quote 0
            • P
              PeterJones @Chris Tanguay 0
              last edited by Aug 19, 2021, 9:46 PM

              @Chris-Tanguay-0 said in Is there a possible way to REGEX "Trim Leading Space"?:

              DO NOT give me the answer

              Okay, but I’ll give hints, since you seem to be getting lost in the docs.

              So I need to come up with a regex, to run on multiple open text files, where each of the text files will have a random # of empty (no printable text) lines at the end (CR LF). Could be anywhere from 2 lines, up to 50.

              So the things you need to figure out how to do are “match the CRLF end-of-line sequence” 🛈 , “match one or more of the previous token” 🛈 , and “match the end of the file” 🛈

              C 1 Reply Last reply Aug 20, 2021, 3:23 PM Reply Quote 1
              • C
                Chris Tanguay 0 @PeterJones
                last edited by Aug 20, 2021, 3:23 PM

                @PeterJones Thank you for the hint. Been looking at the doc last night, and this morning. So, “CR LF” I think translates to \r \n
                So, maybe I’m finding [\r \n]+
                But not sure about the end of file. I see it’s \z, but why do I need to include that?
                Weird that when I turn on the SHOW ALL CHARACTERS, I don’t see any visible representation of end of file (like the CR LF’s).
                OK, yeah, I’m getting lost and overthinking it (or under thinking maybe).
                …
                assume the replace will be a single [\r \n]
                …
                I will do lots of trial and error this weekend! I’ll get there!

                1 Reply Last reply Reply Quote 1
                • S
                  Stefan Pendl
                  last edited by Aug 20, 2021, 5:47 PM

                  Try the menu entry “Edit => Line operations” this will allow trimming leading and trailing spaces as well as removing empty line.
                  Everything is done by NPP and no regex is needed.

                  A 1 Reply Last reply Aug 20, 2021, 5:51 PM Reply Quote 0
                  • A
                    Alan Kilborn @Stefan Pendl
                    last edited by Aug 20, 2021, 5:51 PM

                    @Stefan-Pendl

                    I guess you missed this from the OP’s first posting:

                    So, Notepad++ has the “EDIT —> BLANK OPERATIONS --> TRIM LEADING SPACE” function, which takes care of all those pesky leading spaces & tabbed spaces. But, even as a MACRO, I have to run that function one file at a time, and it’s slower than I’d like.

                    I was hoping there might be a way to REGEX this, so I can use the FIND/REPLACE along with the “REPLACE ALL IN ALL OPENED DOCUMENTS”.

                    1 Reply Last reply Reply Quote 1
                    7 out of 9
                    • First post
                      7/9
                      Last post
                    The Community of users of the Notepad++ text editor.
                    Powered by NodeBB | Contributors