Few Questions about selection, copy & masked paste
-
Hi guys,
so I got some kind of little multi question I would like to bundle into one post if you don’t mind.
1.) How to auto select everything between marker words / chars etc?
I have a large text file and inside are markers to find like this | which represent the start & end markers. Now I would like to find the first marker and the next marker and select them including the text between like this…
Some text here - don't touch it *|* Here all text I want to get selected *|* Some text here - don't touch it *|* Here all text I want to get selected *|*
So in this case npp should select all text between the markers + the markers itself. The text between the markers is of course unknown about length etc. Now imagine I have tons of those markers + text in a document I want to select and delete or copy etc.
2.) How to select desired entire lines using specific markers and how to delete everything else around?
Example: In this case I have again markers inside of the document but just single markers like a specific variable name like CARS: FLY: OEP:. Now I wanna tell npp to find all lines in text using those three marker words and select the entire text lines + optional delete everything else. Some kind of invert selection etc you know.
Some random text here CARS: This line I want to get selected Some random text here Some random text here text and text OEP: This line I want to get selected too Some random text here FLY: This line I want to get selected too Some random text here
Above you can see example text with those markers and I want to select every entire line where a marker was found. Then I wanna copy them to new file (optional) or better is I want to delete all text lines around which would look like this at the end…
CARS: This line I want to get selected text and text OEP: This line I want to get selected too FLY: This line I want to get selected too
3.) How to copy & paste with mask?
Special question. I would like to select and copy (don’t know how to explain it correctly) with some mask. Below a example…
Text AAAAAAAAAAA AAAAAAAAAA AAAAAAAAAA AAAAAAAAAA AAAAAAAAAA
Text B
BBBBBBBBBB BBBBBBBBBB BBBBBBBBBB BBBBBBBBBB BBBBBBBBBB
…so as yo can see I have 2 text files. First one just showing A’s then free line or just spaces and the other text showing same with B’s. Now I would like to select entire Text A copy it and then I go to Text B set the mouse pointer at line 2 (which is empty) and past all from Text A into Text B but without to overwrite Text A and the result would look like this…
AAAAAAAAAA BBBBBBBBBB AAAAAAAAAA BBBBBBBBBB AAAAAAAAAA BBBBBBBBBB AAAAAAAAAA BBBBBBBBBB AAAAAAAAAA BBBBBBBBBB
…which is some kind of masked copy / paste operation I think. So is that doable anyhow with npp? Just keep my previous questions in mind I want to select specific text between markers or entire lines. Now I copy & paste them into new files and now I want also paste other things in that new file at specific positions but without to over paste anything from text before you know. Do you guys have some ideas how to make it work using npp?
4.) Optional question about macros. I was trying to make a own macro to find a marker A in text + start selection + find marker B + end selection. But somehow I don’t get it work because when I search anything in search box then I can’t start a selection with pressed shift key. You know what I mean right? How to make this work? So this question is related to my question 1. Thank you.
-
For question 3, it looks like you’re asking how to merge together two files, where file A has empty lines where file B has non-empty lines, and vice versa. Assuming that’s what you want, this PythonScript script for merging files that I wrote a while back may be helpful for you.
For question 2, if you want to match all text in a line after
UPPERCASEWORD:
, you can use the regular expression(?-si)[A-Z]+:.*
-
@Dean-Corso said in Few Questions about selection, copy & masked paste:
so I got some kind of little multi question I would like to bundle into one post
As you have a number of questions it will likely be that it will take multiple posts to answer them all. Some of the answers are relatively straight forward, others will take some time to explain, with examples. Others still might make reference to other (older) posts, I’m thinking the “interleaving of 2 files’ data”, your question #4.
For question #1, it involves the use to regex (regular expressions). There are plenty of references both in our FAQ section and on the Internet. In short they look like a filter to look for strings of characters. With regex there are a number of characters called meta-characters. These carry special meaning in the expression and to use literally they need to be prefixed with the “escape”
\
character.So looking at your small example of #1 it seems the marker starts at the commencement of a line and finishes at the end of a line, so possibly multiple lines in between. You haven’t actually stated what the end goal is with the selected portions of the file (between markers) is. For my solution I would expect you want to copy only the selected portions to another file.
So using the “Mark” function we have:
Find What:(?s)^\*\|\*.+?(?=\*\|\*)\*\|\*$
Tick Bookmark Line and have the search mode set to “regular expression”. Ideally have your cursor at the start of the file or you need to tick “wrap around”. Click on “Mark All”.At this point you will have the lines you want highlighted and have a coloured ball icon at the start of the highlighted lines. If you right click on one of those balls (actually anywhere in that column between line number if shown and the text) you have an option to copy bookmarked lines from the menu shown. You can also remove those lines at this point, or cut them. Do the copy, open a new tab/file and Ctrl-V to paste them into the new tab.
Question #2. In a very similar manner to the solution for #1 we would again use the Mark function. This time the Find What field would be
(?-i)CARS:|FLY:|OEP:
Tick Bookmark lines, cursor at start or “wrap around” ticked, “regular expression” as the search mode and click on Mark All. So now you will have only those lines with those exact strings marked. Note the (?-i) means “not case insensitive”, which really means exact case so CARS doesn’t equal cars, (?i) is the opposite so any line with “cars:”, “Cars:”, “cArS:” right through to CARS: would all be selected. Again right click on the ball icon in left column and you will also see the option to “remove non-bookmarked lines”. This deletes the unmarked lines, just be aware there might remain 1 final “empty” line in the file that needs removing manually.Terry
-
Thank you for your answer. I tried checking out your PY script but its not what I’m looking for. I’m working with temporary npp tabs and don’t wanna merge files together. In my case I want to do it with selections (copy / paste / masked) you know. So about question 2, your RegEx pattern seems to work to find it but how to get all of them selected? I can mark the matches only etc. Would be nice if there would be any simple npp function for selection too or is this hidden? Did nobody else had that idea before to select all marks too? Sorry, just asking so maybe I didn’t found the function/s yet. But thank you so far of course.
Thank you for your answers so far too. So I tried your steps to see whether they work. First problem is about my question 1. So the markers do not start at the line start so they are anywhere what means I have to change your RegEx pattern to this now…
(?s)\*\|\*.+?(?=\*\|\*)\*\|\*
…and it works as you said but the problem are the bookmarks now. I don’t need the entire lines so I just need to select all marked text portions at this moment but how to select them all at once? I see there is a Copy Marked Text button what does it somehow but not 1:1. I would like to have same positions as original and everything else should be masked with white spaces. In this case a masked copy function would be good if there is any. So otherwise, I would like to remove all marked text OR everything around. So I’m still missing such functions in npp like “Select Marked Text” & “Invert Selection” specially in case of using multi selections you know.
About question 2: In this case it seems to work pretty well using your method with the bookmarks. Thank you for that one. So I hope to get some possibly more answers later about my other questions too.
-
Hello, @dean-corso, @terry-r, @mark-olson and All,
I CANNOT develop right now, because it’s about
02.00 am
, in France and… I’m a bit sleepy !
However, regarding your first question, it can be solved with the following regex :
- SEARCH
(?s)(\Q*|*\E).+?\1\R
Now, regarding your second question, I would use the Mark dialog (
Ctrl + M
) and the following regex :- MARK
(?-i)(?:CARS|OEP|FLY):
Note : don’t forget to check the
Bookmark line
option in order to run, lately, an option of theSearch > Bookmark
menu option
Finally, concerning your third question, here is a real bunch of text for testing ( Beginning of the first chapter of the
Moby Dick
novel ! )I, deliberately, separated all the odd lines, to mimic your
AAAAAAAAAA
lines, and all the even lines, to mimic yourBBBBBBBBBB
linesThus, we get :
Call me Ishmael. Some years ago—never mind how long precisely—having on shore, I thought I would sail about a little and see the watery part regulating the circulation. Whenever I find myself growing grim about I find myself involuntarily pausing before coffin warehouses, and my hypos get such an upper hand of me, that it requires a strong moral methodically knocking people’s hats off—then, I account it high time to With a philosophical flourish Cato throws himself upon his sword; I but knew it, almost all men in their degree, some time or other,
And :
little or no money in my purse, and nothing particular to interest me of the world. It is a way I have of driving off the spleen and the mouth; whenever it is a damp, drizzly November in my soul; whenever bringing up the rear of every funeral I meet; and especially whenever principle to prevent me from deliberately stepping into the street, and get to sea as soon as I can. This is my substitute for pistol and ball. quietly take to the ship. There is nothing surprising in this. If they cherish very nearly the same feelings towards the ocean with me.
Now, for these two blocks of text :
-
Place the caret at the very beginning of their first line
-
Do a zero-width rectangular selection of their
eight
lines ( Hit simultaneously theAlt
andShift
keys and, at the same time, hit seven times on theDown
arrow -
Hit the
End
key and then press theEnter
key
The texts are now changed as below :
Call me Ishmael. Some years ago—never mind how long precisely—having on shore, I thought I would sail about a little and see the watery part regulating the circulation. Whenever I find myself growing grim about I find myself involuntarily pausing before coffin warehouses, and my hypos get such an upper hand of me, that it requires a strong moral methodically knocking people’s hats off—then, I account it high time to With a philosophical flourish Cato throws himself upon his sword; I but knew it, almost all men in their degree, some time or other,
And :
little or no money in my purse, and nothing particular to interest me of the world. It is a way I have of driving off the spleen and the mouth; whenever it is a damp, drizzly November in my soul; whenever bringing up the rear of every funeral I meet; and especially whenever principle to prevent me from deliberately stepping into the street, and get to sea as soon as I can. This is my substitute for pistol and ball. quietly take to the ship. There is nothing surprising in this. If they cherish very nearly the same feelings towards the ocean with me.
-
Now, put the caret right before the word
little
of the first line of the second block -
Again, do a zero-width rectangular selection of their
eight
lines -
While holding down the
Alt
andShift**
keys, select all the contents of the second block, with theRight
arrow -
Use the
Ctrl + C
orCtrl + X
shortcut -
Move the caret on the first empty line, right below the first line of the first block (
Call me Ishmael.
) -
Use the
Ctrl + V
shortcut
=> The beginning of the chapter I should be reconstituted, as below :
Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people’s hats off—then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me.
See you tomorrow, for more details !
Best Regards,
guy038
P.S. : I’ve just seen that you already have some solutions to study, provided by Mark and Terry !
- SEARCH
-
@Dean-Corso said in Few Questions about selection, copy & masked paste:
So the markers do not start at the line start so
Ah, the age old problem of dumbing down the examples so as to make it easier for us to solve. Yet as you found out we haven’t.
It would pay to expand on each of the examples, give us the parameters. Your first expansion is that the first marker of a set doesn’t necessarily start at the commencement of a line. Anything else? Maybe it doesn’t necessarily finish at the end of a line either? Very important and sorry but because of the uncertainty of your actual needs I might just wait on the sidelines.
Terry
PS I did actually change my #1 solution in light of your change in parameters to be
(?s)\*\|\*.+?(?=\*\|\*)\*\|\*
. So can start and finish anywhere on line. Don’t use bookmark lines feature as you dont want entire line. But the selected text will still be highlighted. on the Mark function window so you can copy selected text. However upon pasting that into a new tab you will likely find lines with----
denoting the separate selections you copied. That would then entail more work, so maybe not the right answer either. -
@guy038
Thank you too for the BIG answer. So the RegEx pattern seems not to work when using it in npp and finds nothing etc. The second one seems to work which is similar to @Terry-R pattern.Your answer to question 3 is somehow complicated because I have already the text with empty lines but OK. The second part of your answer really seems to work just using alt+mouse to make the rect selection then strg+C to trim the selection to text and copy and paste it into empty line start of the other text block. Very Good @guy038. :-)
@Terry-R
No no and sorry if I wasn’t clear for 100% with my questions. So the markers on question 1 could be anything and places anywhere in text. Only static value is that there are 2 markers with possibly any text inside I need to select. The marker could be also differently for start & end like S and E for example. So this I need to adjust in your RegEx pattern when using it. The markers could be same or different. Sorry, my RegEx skills are pretty bad because I’m using it rarely and after few months I did forget everything again. -
Hi, @dean-corso, @terry-r, @mark-olson and All,
@dean-corso, you said :
Thank you too for the BIG answer. So the RegEx pattern seems not to work when using it in npp and finds nothing etc. The second one seems to work which is similar to @Terry-R pattern.
Well, I don’t know what happened, but I can assure you that the regex
(?s)(\Q*|*\E).+?\1\R
works fine !
- Place your test block, below, in a new tab
Some text here - don't touch it *|* Here all text I want to get selected *|* Some text here - don't touch it *|* Here all text I want to get selected *|*
-
Open the Search or Mark dialog :
-
Uncheck all the box options
-
SEARCH / MARK
(?s)(\Q*|*\E).+?\1\R?
-
Check the
Wrap around
option -
If you chose the Mark dialog, check also the two options
Bookmark line
andPurge for each search
-
Select the
Regular expression
option
Now, either :
- Click on the
Find Next
button, several times, to get the successive occurrences
or
- Click on the
Mark all
button to mark, at once, all the occurrences
You should get, two distinct selections !
Notes :
-
The
(?s)
syntax means that the regex dot char.
represents absolutely any character ( a standard or EOL one ! ) -
The
\E
and\Q
syntaxes ensures that all text, located between them, is takenliterally
. Indeed,*
and|
have a special signification in regular expressions ! -
The
.+?
syntax represents the smaller range of text, even splitted in several lines, found between the two*|*
boundaries -
The
\1
syntax just stands for the group1
,(\Q*|*\E)
, between parentheses, so the boundary\Q*|*\E
-
The
\R?
syntax represents an optional end of line character(s). So, either the\n
,\r
or\r\n
characters or nothing. Indeed, we must take in account that, possibly, all the searched text is at the very end of the file !
BR
guy038
-
Hello @guy038,
thanks again for another answer. So your RegEx pattern…
(?s)(\Q*|*\E).+?\1\R?
…works now with the question mark at the end. Do you know any simple source to learn / refresh RegEx stuff? I always forget that!
So I also need to find out whether its possible in npp to “find / start selection / find / end selection” anyhow specially when trying to record a macro.
AAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBBB CCCCCCCCCCCCCCCCCCCCC DDDDDDDDDDDDDDDDDDDDD EEEEEEEEEEEEEEEEEEEEE
Above you can see some letters just random. Now I want to find AAAA and if it was found it should select it and as next it should find EEEE and if this was found it should select it. I mean the selection would start at AAAA til EEEE but how to do that manually and to record a macro for that? So I can do the find operations but without the selection part you know. Is it just not doable or is there any special method I could do? How to set & keep the selection enabled after first found to expand it til next found etc? Thank you.
-
@Dean-Corso said in Few Questions about selection, copy & masked paste:
Do you know any simple source to learn / refresh RegEx stuff? I always forget that!
I would go to rexegg.com as it identifies the modifiers in a concise way. Lot’s of other stuff there. Bear in mind it is fairly generic and in some cases the regular expression engine used in Notepad++ won’t fit exactly the examples used. As is the case with most stuff one learns, it can become a bit rusty if not regularly used, however a good grounding will mean you generally know what’s possible even if you don’t know the exact syntax.
I’m trying to understand your next question related to finding the AAAA, then the EEEE and then selecting all in between. The Find function will do all that in one click, so long as the regex pattern fits what you are looking for. The pattern you listed in that post is exactly that sort of pattern you seem to want to use.
Put that pattern into regex101.com and read the description on the right side.
I’m wondering if you have only ever been using the normal search mode where everything is literal and for the exact number as used in a pattern. Regex allows for string lengths (and exact content) searched on to change without having to change or reset the pattern.
There is a FAQ post here that contains many regex references and sources of info. Additionally lower down the post it refers to a 3rd edition of a book with a PDF link to download and read it offline. it is fairly involved so it will take some time to digest all that it contains but it has a great wealth of information.
Terry
-
Hi @Terry-R,
thanks for that website so I will check this out.
About the find / select problem. So I want to create a macro using search function to X and if it was found it should start the selection including X and as second step I want to find Y and if found then it should expand the selection from X to Y…
X anything here Y
…so all that should get selected. Lets say I have some static text / code where I know that X & Y are always there. I want to find & select it and delete that part and this in loop mode. For that I wanna create a simple macro for quickly use if I need it without to think about it again next time and playing around (I forget pretty quick and need to create some auto stuff etc you know). Just have the problem to make that selection + holding it till searching the second variable Y. Just wanna know whether I can handle that with macro or not.
-
@Dean-Corso said in Few Questions about selection, copy & masked paste:
About the find / select problem.
Short answer is yes!
It would seem that you maybe don’t understand what the regex
(?s)(\Q*|*\E).+?\1\R?
is selecting? Have you entered that into the regex101.com site. The description there is fairly detailed and better than I could describe. However I will try to explain in simple terms the components.
(?s)
refers to what the DOT character is allowed to include. In this case it is all characters including the line feed and carriage return ones (essentially end of line). Using this allows for any DOT character in the regex to cross lines, so multi-line capable. That essentially answers one of your questions about how to cover a multi-line selection between AAAAA and EEEEE.The
\Q
and\E
surround characters that we want interpreted as literals, in this example that is the*|*
string. I referred to meta-characters in my first answer to your opening post. This allows those meta-characters to ONLY be interpreted as literals. There is another method, called “escaping” the meta-character which I referred to. Either method is acceptable..+?
means to continue selecting characters in a non-greedy fashion. This combined with the\1
means the regex expands forward in a cautious fashion, testing at each advancement that the following characters aren’t equivalent to the preceding group #1 which is the(\Q*|*\E)
. As soon as it sees in front of the caret (current position in the text) that string, it stops advancing and goes to the next sub-expression in the regex pattern, the\1
which we know is really the(\Q*|*\E)
.The
\R?
refers to the end of line characters, whether they be line feed, carriage return or both. Windows Linux and Mac all use differing sets of these as default for line endings. In Notepad++, go to the Edit menu, then EOL conversion and see the 3 options.Of course in your latest example you would want to select the multi-line string surrounded by AAAAA and EEEEEE. That means the example regex I’ve been referring to would need to be altered. Something like
(?s-i)A+.+?E+
. Note that it looks very different from the previous regex, in fact it is just a variation on that. Firstly I’ve included another modifier, the-i
, this refers to the case of the characters, read that at rexegg.com. As the start and end delimiters (your AAAA and EEEE) aren’t the same character strings we can’t reference group 1. I also removed the \R? requirement as maybe your EEEEE delimiter finishes part way along a line.Perhaps I’ve written enough (more than, I think). You really do need to read up on this and start to have a go yourself. We are here to help but currently I think you need to attempt some regex of your own. Likely you won’t get it exactly right first time, but with time and trials comes the knowledge.
Terry
-
@Terry-R said:
I’m trying to understand your next question related to finding the AAAA, then the EEEE and then selecting all in between. The Find function will do all that in one click, so long as the regex pattern fits what you are looking for.
@Dean-Corso seems very determined to want to do this in two distinct Find operations, for some still-hidden-from-the-rest-of-us reason.
I’d suggest that he post his real-world reason for wanting/needing this, and forget the AAABBBCCCDDDEEE mockup.
-
Hello @Terry-R,
thanks again for that new detailed info’s about RegEx. I was testing a little around and now I see what you mean about that selection thing I would like to do. So it means I just can do it with a RegEx pattern only without any manually mouse move / key buttons. I also did notice that the entire pattern / data I do paste in the search mask gets also saved like that if I record a macro. So before I thought it would be just save every single key press or combo only. That’s a good news for me. Now it’s working to make the selection between 2 markers and do anything with it.
@Alan-Kilborn
What do you mean? Just wanna know and find out answers to the question I have you know.