Reputation: 85
I want to search my code for specific lines and delete them. I'm using regex and find the lines I want to find. By using the Search/Replace function in eclipse, I want to completely delete these lines (they are always multiple ones.) My problem is, that eclipse replaces multiple lines with one empty one instead of deleting it completely. I'm using my search term in the Search field and leaving the replace field empty, as suggested in many other threads. But instead of deleting these lines, eclipse just replaces them with one empty one. I could do it in two steps, first replacing the lines with an empty one and then deleting that empty one using regex, but I have a lot of files which also contain empty lines for structuring/better readability.
Thanks in advance for help!
Example:
Code:
Information
rubbish
rubbish
rubbish
Information
What I want:
Information
Information
What I get:
Information
Information
Upvotes: 2
Views: 936
Reputation: 85
The trick was, that my expression didn't find the newline character and therefore didn't replace it. I just added \R
after my expression an all my problems were solved.
Many thanks to howlger for the hint and thanks to Sweeper for the idea of searching for a newline character.
Upvotes: 3
Reputation: 270980
Suppose you want to remove lines that says rubbish
, you can just add a \n?
(or whatever your OS's new line character is plus ?
) at the end:
rubbish\n?
This will match any new line character that is after rubbish
and the new line will be replaced. You can replace rubbish
with whatever pattern you want.
However, this leaves an empty line at the end if the last line is rubbish
, because the last rubbish
doesn't have a new line following it. This shouldn't be a problem unless you are removing lines that contain }
, which often is the last line in a Java source file.
To overcome this problem, you can use this regex:
rubbish\n|\nrubbish
which is kind of slow, but that shouldn't be a problem as you are not writing production code with this regex. You are just trying to refactor your source code, right?
Using rubbish\n
then using \nrubbish
can be faster.
Upvotes: 1