search-and-replace NOOB question - CR and (any number)
-
Fellow Notepad++ Users
Could you please help me with the following search-and-replace problem I am having?
I have a bunch of large files with time-stamps between lines.
I would love to build a search-replace string with Search:
= (carriage return or line feed)(any number)(any number)(colon)(any number)(any number)(carriage return or line feed)
and Replace:
= (space)
should be really simple, but the online User Manual - well, I have not been able to find any descriptors that fit, nor any examples close to my needs.This is embarrassing!
I can almost see how to do most of it but can’t get it to work.
I can replace numbers - did a trial for “11:” and replaced successfully with (space).
Then tried (carriage return) to (space) and got a space at the end of the line, but did not remove the (carriage return).
Note - I know it needs to be Extended Search Mode.Unfortunately, nothing I have tried works as I desired, and I’m not sure why. Could you please help me understand what went wrong and help me find the solution?
-
@JEFerris-99999 said in search-and-replace NOOB question - CR and (any number):
I would love to build a search-replace string with Search:
= (carriage return or line feed)(any number)(any number)(colon)(any number)(any number)(carriage return or line feed)
and Replace:
= (space)
[…]
Note - I know it needs to be Extended Search Mode.You need Regular expression mode, not extended search. Regular expressions are a little complicated, but worth learning if you need to search for patterns. For your case, what you need in the Find field is:
^\d\d:\d\d\RNow, there is a small consideration. Do you want the line removed, replaced with an empty line, or replaced with a line containing a single space? (You probably didn’t mean that you wanted to replace the line endings before and after the time stamp, as that would merge the two lines on either side into one.)
The expression above, with an empty replace field (not even a blank) will remove the lines with the timestamps. If you want to leave the lines and just replace the timestamp with either an empty line or a blank, use:
^\d\d:\d\d$and put either nothing or a single blank in the Replace field.
If you really meant exactly what you wrote, and you want to remove the timestamps and the line breaks on either side (putting the two lines on either side into one, with a blank between them), use:
\R\d\d:\d\d\R -
@JEFerris-99999 said in search-and-replace NOOB question - CR and (any number):
Note - I know it needs to be Extended Search Mode.
This is the first part of the problem. If you want to match “any number”, then Extended Search Mode isn’t sufficient: you need to use Regular Expression mode, not Extended Search Mode.
= (carriage return or line feed)(any number)(any number)(colon)(any number)(any number)(carriage return or line feed)
and Replace:
= (space)
should be really simple, but the online User Manual - well, I have not been able to find any descriptors that fit, nor any examples close to my needs.- Searching > Regular Expression > Character Escape Sequences:
\dmatches any digit (0-9), which is what I am interpreting your “any number” to mean - Searching > Regular Expression > Special Control Escapes:
\Rmatches a newline sequence in a FIND WHAT, whether you are in Windows (CRLF), Unix/Linux (LF), or ancient Mac (CR) line-ending mode; it will match the whole newline sequence, not just one byte from it, to make sure that your replacement works as you expect it to. (In practice, it essentially matches\r\n|\r|\n, though technically it matches(?>\r\n|\n|\x0B|\f|\r|\x85|\x{2028}|\x{2029}))
I am assuming that the
=you show was you saying “my expression equals…” rather than meaning a literal = sign before the newline. If so, then FIND WHAT =\R\d\d:\d\d\Rwould matchcarriage return or line feed, any digit, any digit, colon, any digit, any digit, carriage return or line feed.For the replacement, just type the space character (or, if you want to be fancy, use
\x20to reference ASCII hex20 = decimal32 = space character)Putting it all together:
- FIND WHAT =
\R\d\d:\d\d\R - REPLACE WITH =
\x20(or just a space, but I cannot show a space character in thered text - SEARCH MODE = Regular Expression
Unfortunately, nothing I have tried works as I desired, and I’m not sure why. Could you please help me understand what went wrong and help me find the solution?
- “Extended” is the wrong mode to be able to do generic digits rather than just specific numbers
- If you just matched the LF of the CRLF sequence, and replaced that with a space, then you would still keep the CR, which Notepad++ would still display as a newline. Turning on View > Show Symbol > Show End of Line and Show Space and Tab would help you see exactly what’s happening with the end of line characters and spaces/tabs, where a carriage return
\rshows up asCRin a black box, line feed\nasLFin a black box, and the space will be a mid-dot and the tab a right-arrow.
- Searching > Regular Expression > Character Escape Sequences: