Community
    • Login

    Few Questions about selection, copy & masked paste

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    14 Posts 5 Posters 520 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Dean-CorsoD
      Dean-Corso @Terry R
      last edited by

      @Mark-Olson

      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.

      @Terry-R

      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.

      Terry RT 1 Reply Last reply Reply Quote 0
      • guy038G
        guy038
        last edited by guy038

        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 the Search > 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 your BBBBBBBBBB lines

        Thus, 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 the Alt and Shift keys and, at the same time, hit seven times on the Down arrow

        • Hit the End key and then press the Enter 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 and Shift** keys, select all the contents of the second block, with the Right arrow

        • Use the Ctrl + C or Ctrl + 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 !

        1 Reply Last reply Reply Quote 2
        • Terry RT
          Terry R @Dean-Corso
          last edited by Terry R

          @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.

          Dean-CorsoD 1 Reply Last reply Reply Quote 1
          • Dean-CorsoD
            Dean-Corso @Terry R
            last edited by

            @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.

            1 Reply Last reply Reply Quote 0
            • guy038G
              guy038
              last edited by guy038

              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 and Purge 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 taken literally. 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 group 1 , (\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

              Dean-CorsoD 1 Reply Last reply Reply Quote 1
              • Dean-CorsoD
                Dean-Corso @guy038
                last edited by

                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.

                Terry RT 1 Reply Last reply Reply Quote 0
                • Terry RT
                  Terry R @Dean-Corso
                  last edited by

                  @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

                  Dean-CorsoD Alan KilbornA 2 Replies Last reply Reply Quote 2
                  • Dean-CorsoD
                    Dean-Corso @Terry R
                    last edited by

                    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.

                    Terry RT 1 Reply Last reply Reply Quote 0
                    • Terry RT
                      Terry R @Dean-Corso
                      last edited by

                      @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

                      Dean-CorsoD 1 Reply Last reply Reply Quote 3
                      • Alan KilbornA
                        Alan Kilborn @Terry R
                        last edited by

                        @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.

                        1 Reply Last reply Reply Quote 2
                        • Dean-CorsoD
                          Dean-Corso @Terry R
                          last edited by

                          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.

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post
                          The Community of users of the Notepad++ text editor.
                          Powered by NodeBB | Contributors