Is there a possible way to REGEX "Trim Leading Space"?
-
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 -
Regular expression mode, FIND
^\h+
(beginning of line, followed by 1 or more horizontal spaces (basically, spaces and tabs) and REPLACE with empty -
@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) -
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.
-
@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:
to this:
-
@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” 🛈
-
@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! -
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. -
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”.