Bug in Notepad++ / BOOST or bug in my regular expression? - regex

I have a file which is structured like this:
Line
foo Änderbar: PM baz
Line
Line
foo Änderbar: OM baz
Line
Line
foo Änderbar: ++ baz
Line
Line
foo Änderbar: -- baz
Line
So the file consists of "blocks" which are separated by a newline (I have converted the file to Unix line endings). Each block can have an arbitrary number of lines. Each line of a block contains at least one character which is not a newline, and is finished by a newline character. The lines which separate the blocks consist of exactly one newline character.
In each block, there is exactly one line in the following format:
at least one character which is not newline, followed by
the literal string 'Änderbar: ', followed by
exactly one of the literal strings '++', '--', 'OM', 'PM', followed by
at least one character which is not newline, followed by
the line-terminating newline character
There is always at least one other non-empty line in the same block above this special line and one other non-empty line below this special line.
I need an effective method to find (and thereby select) all blocks where the literal after Änderbar: is -- (find / select one block after another, each one after hitting Find Next again, i.e. not selecting all of those blocks at the same time).
Normally, I have fun solving such problems with Notepad++. However, in that case, it seems that I either get more and more stupid as I get older, or that there is a bug in Notepad++'s regex handling engine.
Notepad++ uses BOOST (and supports PCRE expressions via BOOST). Since this is in wide use, I consider that problem important enough to post it here, just in case that BOOST really is the reason for the misbehavior.
Having said this: I loaded that file into Notepad++, fired up the Search and Replace dialog, ticked . matches newline, ticked Regular Expression and entered the following regex in the Find What: textbox:
\n([^\n]+\n)+[^\n]+(Änderbar\:\ --[^\n]+\n)([^\n]+\n)+
I was quite surprised that this made Notepad++ behave weirdly: When the cursor was placed in the empty line immediately before a block with Änderbar: --, hitting Find Next found / selected that block as expected. But when the cursor was at another place, hitting Find Next made Notepad++ find / select the whole rest of the file, i.e. all blocks below the cursor position.
I then have tested if it would find the blocks having ++ after Änderbar:, i.e. I changed my regex to
\n([^\n]+\n)+[^\n]+(Änderbar\:\ \+\+[^\n]+\n)([^\n]+\n)+
Guess what: This was working reliably in each situation. The same is true for the last both:
\n([^\n]+\n)+[^\n]+(Änderbar\:\ PM[^\n]+\n)([^\n]+\n)+
\n([^\n]+\n)+[^\n]+(Änderbar\:\ OM[^\n]+\n)([^\n]+\n)+
So Notepad++ / PCRE seems to have a problem with the correct interpretation of - under certain circumstances, or I have a subtle bug in my regex which only triggers when I am searching for -- (instead of ++, OM or PM) at the respective place.
Please note that I already have tried to leave away the \ in front of the space character (which actually could only make the situation worse, but I've tried just in case) and that I also have tried to use \-\- instead of -- (although the latter should be fine). That did not alter the (mis-)behavior in any way.
So what is the problem here? Is there a bug in my regex, or is there a bug in Notepad++?
UPDATE
I have stripped down the actual file in question and have uploaded it to https://pastebin.com/w62E57U5. To reproduce the problem, please do the following:
Download the file from the link above and save it somewhere on your HDD (do not copy the text directly into Notepad++).
Load the file into Notepad++. The cursor now is in the topmost line, and nothing is selected.
This is essential: Click Edit -> EOL Conversion -> Unix (LF).
Verify that the cursor is still in the topmost line (which is empty) and that nothing is selected.
Open the Find dialog and choose the settings and enter the search string as described above.
Click "Find Next".
Note that now the complete text is found / selected.
Keeping the Find window open, delete the third line of the file (it reads "Funktionspaket(e): ML"). Do not just empty that line, but really delete it so that no empty line remains between the line before and the line after.
Again, place the cursor in the topmost line (which is still empty) and make sure nothing is selected.
Click "Find Next".
Note that the regular expression now works as expected.
Obviously, somebody is trying to make a fool of me, right?

I think the key is: you need to begin your regex with ^ (beginning of line).
Your original regex becomes:
^\n([^\n]+\n)+[^\n]+(Änderbar\:\ --[^\n]+\n)([^\n]+\n)+
But you can simplify it with:
^\R(?:.+\R)+.+Änderbar: --.+\R(?:.+(?:\R|\z))+
Note: tick . matches newline
Where:
\R matches any kind of linebreak, no needs to change the EOL.
\z matches the end of file, if you don't use it, you can't match the last line of the file if there're no linebreak.
(?:...) is a non capture group, much more efficient (if you don't need to capture, of course)
Both works fine with your 2 sample files.

It's not a bug. You're just forgetting something very important - with Windows line endings, your lines have a \r before the \n, so the \n([^\n]+\n)+ part of your RegEx will also match your blank lines which is why clicking "Find Next" matches everything from the cursor position instead of from the start of the block.
Go to Edit > EOL Conversion > Unix (LF) and you'll see that it works now. If you want to support Windows and Unix line endings you'll have to change every [^\n] to [^\r\n] and every \n to \r?\n.

Related

RegEx for underlining text

How can I match one line of text with a regex and follow it up with a line of dashes exactly as many as characters in the initial match to achieve text-only underlining. I intend to use this with the search and replace function (likely in the scope of a macro) inside an editor. Probably, but not necessarily, Visual Studio Code.
This is a heading
should turn into
This is a heading
-----------------
I believe I have read an example for that years ago but can't find it; neither do I seem to be able to formulate a search query to get anything useful out of Google (including variations of the question's title). If you are I'd be interested in that, too.
The best I can come up with is this:
^(.)(?=(.*\n?))|.
Substitution
$1$2-
syntax
note
^(.)
match the first character of a line, capture it in group 1
(?=(.*\n?))
then look ahead for the rest of this line and capture it in group 2, including a line break if there's any
|.
or a normal character
But the text must has a line break after it, or the underline only stays on the same line.
Not sure if it is any useful but here are the test cases.

Deleting lines with specific words in multiple files in Notepad++

I'm trying to removing a specific line from many files I'm working on with Notepad++.
Upon searching, I found Notepad++ Remove line with specific word in multiple files within a directory but somehow the regex provided (^.*(?:YOURSTRINGHERE).*\r\n$) from the answers doesn't work for me (screenshot: https://cdn.discordapp.com/attachments/311547963883388938/407737068475908096/unknown.png).
I read on some other questions/answers that certain regex doesn't work in newer/older Notepad++ versions. I was using Notepad++ 5.x.x then updated to the latest 7.5.4, but neither worked with the regex provided in the question above.
At the moment I can work around it by replacing that line with nothing, twice (because there are only 2 variants that I need to remove from those files) but that leaves an empty line at the end of the files. So I have to do another step further to remove that empty line.
I'm hoping someone can offer helps that allow me to remove that line and leave no empty line/space behind.
The regex you attempt to use will only match your line, if it is followed by an empty line and Windows linebreaks (CR LF) are used. This is due to \r\n$ which matches a linebreak sequence followed by the end of the line.
Instead you might want to use
^.*(?:YOURSTRINGHERE).*\R?
To match the line containing your string and optionally a following line break sequence to remove the line instead of emptying it out. This will leave you with a trailing newline, if your word is contained in the last line of a file. You can use
(\R)?.*(?:YOURSTRINGHERE).*(?(1)|\R)
To avoid this. It uses a conditional to either match the previous linebreak, or the following if there is none.

Global find and replace with newline in Visual Studio Code

Suppose I want to remove all lines matching a regex in my project. Is there a way to do that?
Using the global find and replace function with regexes enabled I've tried:
Replace foo|bar with an empty string. This doesn't work because it leaves the line there with an empty string. I want the newline removed.
Replace (foo|bar)\n with an empty string. This doesn't actually match anything.
Replace (foo|bar)$ with an empty string. Again, doesn't match anything.
Any ideas?
Edit: It seems like some of my files have Windows line endings so (foo|bar)\r?\n does match. However when you replace it with an empty string it actually still leaves the line endings there.
Here's a test case:
a
foo
b
It should end up like this:
a
b
Not like this:
a
b
foo\n^ and (foo|bar)\n^ both work.
I just tested in my vs code - and you leave the replacement string blank
Yes, it is possible to remove entire lines with the search-across-files feature.
I'm guessing the original problem was due to a bug or otherwise unwanted behavior in an older version of VSCode. With VSCode 1.37.1, so long as the \n is included in the regex, the line is removed. In particular, the regex (foo|bar)\n, described in the original question as not working, now works fine.
Before:
After pressing the "Replace All" button:
Related observations:
This same regex works even if I set the file line endings to CRLF.
Appending ^ makes no difference. That's a bit surprising, but perhaps "after newline" counts as "beginning of line".
Appending $ causes the regex to not match anything. That is quite surprising given the behavior of ^.
I looked through the search configuration settings, but nothing seemed like it could affect this.

How can I collapse multiple whitespace lines with vim?

The question and answers here cover in detail how the following vim command collapses a series of empty lines into a single line:
:g/^$/,/./-j
However, I want to do the same but also treat lines with onlywhite space in them as blank. The following command is what I tried but it doesn't work:
:g/^\s*$/,/./-j
As far as I can tell, that should find the lines that are empty and have only whitespace on them, but not all lines are being collapsed.
You're halfway there.
Remember that the initial command consisted of a search part and an action part. The search part :g/^$/ found all empty lines and the action part ,/./-j was executed for each (well, each that hadn't already been deleted by a previous j).
The modification you made to the search part of the string is correct in that it will now find lines that are either empty or contain only whitespace.
However, it's the action that you're executing after that that's causing you grief. The original action to be executed on the found line was ,/./-j which basically means execute a join j over the range from this line to the one before the next 'real' character. More detail on how this works can be found in the question you linked to.
The first 'real' character that it finds in your case actually includes whitespace so, while the search bit will find whitespace lines and act on them, the range of the join in the action will not be what you want.
What you need to specify for the end of the range in the action is the line previous to the next one that has something other than whitespace (rather than just a line with any 'real' character). A line with a non-whitespace character is simply one that matches the regex \S (the backslash with uppercase S denotes a non-whitespace character).
So, in the end, what you're looking for is:
:g/^\s*$/,/\S/-j
Having said that, keep in mind that the line that remains behind is (I think) the first from the range. So, it's not necessarily empty, it may contain white-space.
If you wish to ensure all whitespace-only lines are made empty, just execute:
:g/^\s*$/s/.*//
after the collapsing command above. Or, you can combine both into a single command using | as an action separator:
:g/^\s*$/,/\S/-j|s/.*//

why :%s/^$//g is not equal to :g/^$/d in vim?

I want to delete blank lines in the file in gvim for windows (not vim from cygwin).
:g/^$/d # delete all blank lines correctly
:%s/^$//g #can not delete all blank lines
It's a problem how to express the end of line ,
i have checked my file in detail with the command %!xxd ,
i found there is a 0d0a at the end of every line,
when the end of line is expressed by the special character $ ,
does it contain the 0d0a?
it is different to express concept of the end of line betwwen command g and s ?
:%s/^$\n//g #delete all blank lines correctly
It confused me that ^$ will contain the special character \r\n or not,maybe ^$ in s command do not contai the special character \r\n ,but ^$ in g command do contai the special character \r\n.
which position does special character $ point at ? behind the \r\n or before the \r\n.
No, the two commands are doing different things. :g/^$/d says "find empty lines and delete them", while :s/^$//g means "in this (current) line, replace all occurences of nothing-on-a-line with nothing (still on the same line)". The latter, as you notice, does not make sense.
EDIT for your edit:
The ^$ do not contain anything. It literally says "start of line, then immediately end of line", where "line" is detected by Vim. Vim knows that \r\n or \r or \n (depending on file and on which OS it was created, and on Vim's options) mark the end of line, and treats it accordingly. The separator lies between the ^$.
It's like when you say "the rooms in my house" - this (for me at least) does not include walls. When you say ^$\n, you're saying "the empty room, and also the wooden wall next to it". s/^$// is "empty the (already empty) room"; s/^$\n// is "empty the empty room and break down its wall".
In contrast, for g, again it does not say anything about \n. It finds the empty row (not caring about any separators), and then does a command on it; in your case, the command is d: delete a row. It deletes a full row (along with any newlines). For example, if you write :g/DELETEME/d, it would delete any rows that have DELETEME in it, anywhere: it does not care about any newlines in the match part, it just deletes the matched rows.
d means delete the lines that are matched. s is just a substitution. Essentially the second expression means "substitute lines that match ^$ with an empty string," but that does not delete them. You are substituting nothing with nothing. ^ and $ are zero-width assertions and cannot be replaced.
Both #Amadan and #Explosion Pills are right, but I think you really need an explanation of "how vim thinks" about what you are editing.
For one thing, vim is based on vi, which was a front end to the ex editor. Although vim has come a long way, it is still a line-based editor. Do not look for the magic character at the end of the line, because there is none! (When I say that vim has come a long way, I remember when the 'whichwrap' option was added.)
We usually do not bother to make the distinction, but the file on your hard disk is different from the buffer that you edit in vim (:help buffers). The file is a sequence of characters (or bytes). When vim reads the file, it splits them up into lines depending on the 'fileformat' option (short form 'ff'). Since you work on windows, the default is 'ff'=dos, which means that 0d0a (a.k.a. CRLF or \r\n) in the file is used to separate lines. I have 'ff'=unix, so I see only 0a when I filter through xxd. (Before OS X, Mac used 0d, so there were three standards!)
It is a good thing that the magical EOL character is simply not there, because that makes vim portable between systems. Being a line-based editor, vim lets you do all sorts of things like find the first pattern match on each line and do something to it. That is not always what you want, so some people get in the habit of adding /g at the end of every :s command, or even set 'gdefault'.
Coming back to your original question, removing a line from the buffer is very different from removing all the characters in the line. Ex commands (Remember the lineage) act on lines, and :d is the command to delete one. The :s command will change a line; do not be confused by the common usage :%s, which invokes :s on every line in the buffer (:help :range).