I was working with some logs in which there are several separators for each information field, e.g.:
********** Field #1 **********
Content inside Field #1
More content
********** Field #2 **********
Content inside Field #2
More content
...
********** The last field will always remain unchanged **********
Unchanged content from last field
Periodically, I have to delete all the content from the respective fields and manually provide the new data that is going to occupy that space. The problem is that the logs are way too long to select and delete all of that content by hand, so I wrote a RegEx in Notepad++
find/replace to detect the end of a separator *
and subsequent lines with \r\n
until it bumps into another *
.
Here follows my expression:
(?<=\*)([^\*]+\r\n)(?=\*)
How it works:
*
from a group of stars/asterisks separator;*
.As you may have read in the log example, the last field must stay unchanged, no matter what. So I am struggling to match the exact place after the last field. I tried putting some unique reference from the last field's content inside the negated \*
matching list in group 2,but no success.
Currently, the solution I wrote works well with all fields, but I wanted to make it regarding the condition that the last field must stay the same and be able to Replace All
without changing last field. Is there any way we can work with the existing solution and improve it? If not, is there another different solution for this case?
Thank you so much in advance for any help.
📢 Update: no content field can contain
*
stars/asterisks, also, the number of*
stars/asterisks can vary from field to field. They are being used only for the purpose of separating the different information inside the log file.
My intention is to use this rule and replace the matched content by \n\n
in find/replace. It will produce something like this:
********** Field #1 **********
********** Field #2 **********
...
********** The last field will always remain unchanged **********
Unchanged content from last field
You could match a line starting and ending with an asterix and then forget what is matched so far.
The match all lines to delete that do not start with an asterix
^\*.*\R\K.*(?:\R(?!\*).*)*\R(?=\*)
The pattern matches:
^
Start of string\*.*\R
Match *
followed by the rest of the line and a newline\K
Forget what is matched so far.*
Match the whole line(?:\R(?!\*).*)*
optionally repeat matching all lines that do not start with an asterix\R
Match a newline(?=\*)
Positive lookahead, assert *
to the rightThen replace with your content followed by a newline.