Regex plese help - how to remove every 2nd and 3rd
-
Hi Friends
please help me how to manage this
I want to remove every send and third line in my text file without leaving a blank line.This is my file:
Login: xxxx Lng: Java:version- xxxx Code: xxx Login: xxxx Lng: Java:version- xxxx Code: xxx Login: xxxx Lng: Java:version- xxxx Code: xxx Login: xxxx Lng: Java:version- xxxx Code: xxx
And i want to see
Login: xxxx Login: xxxx Login: xxxx Login: xxxx
-
@Martin-X said in Regex plese help - how to remove every 2nd and 3rd:
I want to remove every send and third line
As your file looks to only contain 3 types of lines, why not just sort the lines so all login lines are together. Then select all others and remove them.
Terry
-
@Terry-R makes a good point, but if the OP really wants to use a regex replacement, I’d suggest:
Find what box:
(?-s)^(Login:.+\R).+\R.+\R
Replace with box:\1
Match case checkbox: ticked
Search mode radiobutton: Normal -
thank you
your regular expression works but the last two lines remain
Why it not remove them?before
after
-
@Martin-X said in Regex plese help - how to remove every 2nd and 3rd:
Why it not remove them?
The reason is that the regex HAS to find 3 lines, all with end of line markers
\R
. Unfortunately the last line has a end of file marker instead. You will note that in your example it found 3 occurrences, not 4 as you should have expected.
Another way to achieve what you want is to use the Mark function. TypeLogin:
and make sure “bookmark line” is ticked. As the search text is constant this search can be done in normal mode even. Once the lines are marked (generally with a blue circle at start of line) you can then remove unmarked lines by going to Search\Bookmark\Remove unmarked lines. I did notice in a quick test that this also produces a side effect of leaving a blank line at the bottom of the file.Terry
PS I note @Alan-Kilborn post stated search mode normal, however you had it as regular expression. Thats good as his post had that mistake but obviously you saw past that.
-
@Terry-R said in Regex plese help - how to remove every 2nd and 3rd:
I note @Alan-Kilborn post stated search mode normal, however you had it as regular expression. Thats good as his post had that mistake but obviously you saw past that.
Arrrgh. Apologies for the (obvious) mistake!
Unfortunately the last line has a end of file marker instead.
Yea, if we’re just talking about one file, just handle the last lines manually.
If we’re talking thousands of files, though, you should state that initially and a solution that handles either kind of file ending can be developed.
Or…use the editorconfig plugin and make sure all files are properly terminated with a line-ending (my preferred method). -
In this instance it isn’t that difficult to craft the solution that handles a file-ending of either type:
Find what box:
^(Login:.+\R).+\R.+(?:\R|\z)
Replace with box:\1
Match case checkbox: ticked
Search mode radiobutton: Regular expression -
Whank You very much :) works like a charm.
But I want to ask for another example.
How to change this regex^(Login:.+\R).+\R.+(?:\R|\z)
if I want to get this effect:
xxxx
xxxx
xxxx
xxxxinstead of:
Login: xxxx
Login: xxxx
Login: xxxx
Login: xxxx -
Well, we ask people to “take away a bit of learning” from every piece of advice provided here.
Given that, what might you think would be a modification to what was given earlier that would do what you now need?Unrelated: Using the previous resulting text as the new starting point, you could always search for
Login:
and replace with nothing. :-) -
trust me i try to learn something
i spent today about 3 hours to find solution for various text files
but this last one was to difficult for meNow I want to make it in one regex to save some space
So i want to use one regex command to delete second and third line and “Login:”
and leave only
xxxxCan You help me one more time?
-
@Martin-X said in Regex plese help - how to remove every 2nd and 3rd:
So i want to use one regex command to delete second and third line and “Login:”
I will give you some guidance. If you look at @Alan-Kilborn regex you will see the line that is written back is enclosed in brackets in the Find What line. The replace with line is
\1
which is how we write back that information within the bracket. By changing the contents of the bracket (essentially moving the bracket to the right) you will change what the regex keeps.
Also note that you may have overlooked the space in the line you want to keep, now removing the front portion, so keep that in mind when editing his regex.Terry