How to replace a whole line starting with a given expression
-
Hello
I would like to remove all lines in a NotePad++ text file starting with a given word.
These lines may vary in the number of caracters. Exemple : I want to remove all lines starting withX-Gmail-Labels: (and then, the length of the line as well as its content may vary)
Such as :
X-Gmail-Labels: =?UTF-8?Q?Messages_envoy=C3=A9s,Bo=C3=AEte_de_r=C3=A9ception?=What to I write in the Search box ?
Thanks !
Rachad -
by using regular expression and
/s*GIVEN_WORD.*in find what, keep replace with empty you can replace all lines.
For details about the regex see here.
Cheers
Claudia -
Ok, Claudia, this time you have really lost me. I don’t understand your solution! :-(
Here’s mine:
Find-what zone:
(?-s)^X-Gmail-Labels:.*\R
Replace-with zone: make sure this is EMPTY
Search-mode:Regular Expression
Wrap-around checkbox:ticked
Action: Press Replace-All buttonThat is tailored to your example, Rachad. Obviously(?) the general solution is:
Find-what zone:
(?-s)^givenword.*\R
(all else the same) -
Hi Scott,
I’m puzzled too, where is the circumflex and why is there a forwardslash instead of a backslash??I thought I wrote
^\s*GIVEN_WORD.*Strange.
I added the \s just in case it is in front of GIVEN_WORD - might be not exactly what OP is asking for.Thank you for pointing this out.
Cheers
Claudia -
Hello @rachad-antonius, @claudia-frank, @Scott-sumner, and All,
Claudia, don’t forget that the
\sregular syntax matches any single character of the range[\t\n\x0B\f\r\x20\x85\xA0\x{2028}\x{2029}]So let’s imagine this initial text, below :
-
Which begins with 3 empty lines
-
With 2 other empty lines, after line bbbbbbb
-
And the second to last line contains, ONLY the expression GIVEN_WORD
GIVEN_WORD aaaaaaaaaaaa bbbbbbb GIVEN_WORD ccccccccc GIVEN_WORDeeeeeeeeeeeeeeeee GIVEN_WORD fffffff gggggggggggggggggg hhhhhhhhh GIVEN_WORD iiiiiiiiiiiiiiiiiiiiiiiiiiiii GIVEN_WORD jjjjjjjjjjjjjjjjjjjjjjWith your regex,
^\s*GIVEN_WORD.*, this text turns into :bbbbbbb gggggggggggggggggg hhhhhhhhh jjjjjjjjjjjjjjjjjjjjjjWhereas the Scott’s regex,
(?-s)^GIVEN_WORD.*\Rgives :bbbbbbb gggggggggggggggggg hhhhhhhhh jjjjjjjjjjjjjjjjjjjjjjThis later syntax seems more correct as it matches a single line, at a time ;-)) doesn’t it ? Claudia, you may click several times on the couple
Find Next | Replace, with the View > Show Symbol > Show All Characters option set !BTW, @rachad-antonius, the
\Rsyntax, at the end of the regex, matches the line-break characters of the current line, whatever the kind of file (Unix,Windows, orMac) !
Additional Notes :
-
In case, @rachad-antonius, that you would like, in addition, to delete entire lines, having some blank characters ( spaces and/or tabulations ), before the GIVEN_WORD, you could use the regex
^\h*GIVEN_WORD(?-s).*\R -
Secondly , depending if you prefer the GIVEN_WORD to be matched with that exact case, or not, you could choose :
-
The regex
^\h*(?-i)GIVEN_WORD(?-s).*\Rfor an exact case match -
The regex
^\h*(?i)GIVEN_WORD(?-s).*\Rfor a match, independent of the case of GIVEN_WORD
-
-
Finally, if GIVEN_WORD represents an complete expression, containing special regex characters, I advice you to use the following one :
^\h*(?-i)\QGIVEN_WORD\E(?-s).*\R. Indeed, between the 2 syntaxes\Qand\E, you may insert any character or symbol, without no restriction !!
Refer to :
For instance, if you want to delete the entire lines, beginning with the string +++ | Test | +++, whatever the case of the word test, and possibly preceded, in the same line, by some blank characters, use the regex, below :
^\h*(?i)\Q+++ | Test | +++\E(?-s).*\RRemark :
If you don’t use the quoted form
\Q.......\E, the regex must be rewritten as below, which each special regex character escaped with an\character !^\h*(?i)\+\+\+ \| Test \| \+\+\+(?-s).*\RBest Regards,
guy038
P. S. :
Claudia, many Thanks for your Xmas Greetings ! Sorry for not replying, in time :-< I just forgot to consult my e-mail box ;-))
-