Find text, copy and paste to another file



  • Is there a way to write a macro or save key strokes to do the following: (I’m not sure how wildcards “*” work in Search in NPP)

    Two NPP files are open. Start the macro with cursor at the first position in File 1.

    1. Find “Mailto:*” (quotes are part of the searchable text)
    2. Copy highlighted text
    3. Switch to File 2
    4. Paste clipboard contents
    5. Switch to File 1
    6. Space forward

    Any help would be greatly appreciated. It’s been many years since I’ve done any coding.
    Thx!
    ~Scott P



  • @Scott-Piker
    Whilst this is not actually giving you the solution you are after it might still be helpful.

    Depending on the length of the lines containing the Mailto text you are seeking, you can use the “Mark” function under Search (main menu). In the find what field type the text in so far as the explicit (non-changing portion). So likely "Mailto:
    Make sure you tick the box ‘bookmark line’, then mark all. All lines with that text then have a marking in the left column.
    From there you use the Bookmark option (under Search - main menu) and select ‘copy bookmarked lines’. Open a new tab and paste the lines in there.
    From there, depending on what other text is on the same line it will require some further editing.

    I’m showing you this as it might be a lot simpler to work through if you aren’t used to coding.

    Otherwise you could perhaps give us a sample of the file, changing out private text as needed (we’d only need a few lines). The idea being we could provide you with a regex to remove all other text in the file leaving just the relevant information you seek. You’d do this on a copy of the original file.

    Terry



  • This is very helpful! Thank you! This definitely saves me some time.

    But, yes I would definitely like to write a macro or “regex” to do this if possible. I would like to capture the email addresses from some HTML code and write them to a new file so I can easily build distribution lists for various teams in my org. Basically, I want to capture all text between the quotes keying off of the characters, mailto. Then write the addresses to a new file. Or, as you mention, eliminate the text I do not want.

    Example:

    text text text text “mailto john.doe1@xyzcorp.com” text text text text text text text “mailto: john.doe2@xyzcorp.com” text text text text text text text “mailto: john.doe3@xyzcorp.com” text text texttext text text text “mailto: john.doe4@xyzcorp.com” text text text
    text text text text “mailto: john.doe5@xyzcorp.com” text text texttext text text text “mailto: john.doe6@xyzcorp.com” text text text text text text text “mailto: john.doe7@xyzcorp.com” text text texttext text text text “mailto: john.doe8@xyzcorp.com” text text text



  • @Scott-Piker said:

    Find “Mailto:*”

    I been thinking a bit more about your query and think I have a good (easy to understand) answer to solve it. In english it just involves first making a copy of the file, then on the copy we find the instances of "Mailto:text" and surround them with carriage return/line feeds. Once this is completed use the “Mark” function as I suggested earlier to select the lines that will ONLY contain "Mailto:’ text. Then under bookmark we can remove ‘unmarked’ lines.
    So as an example I produced this small number of lines of gibberish which also contains the text on some lines in different positions.

    <item parent="9463" "Mailto:xyz@3.com"index="9476">
        <block lx="2.34999752" ly="6.58951187"
    "Mailto:xyz@3.com" lz="16.3749924" ux="2.39999676" uy="6.63951397" uz="16.8749962" index="151"
    material="3" look="0" up="3" color="ffcd0000"/>"Mailto:xyz@3.com"
    </item>
    "Mailto:xyz@3.com"<item parent="8956" index="2458"> "Mailto:xyz@3.com"
    <block lx="2.34999752" ly="6.58951187"
    lz="16.3749924" ux="2.39999676" uy="6.63951397"
    uz="16.8749962" "Mailto:xyz@3.com" index="200" material="3" look="0"
    up="3" color="abcdef"/>
    </item>
    

    I used the “Replace” function with:
    Find What:(?i-s)("Mailto:[^"]+")
    Replace With:\r\n\1\r\n
    Have search mode set as “regular expression” and wrap around ticked. Hit the “Replace All” button. Afterwards (my example) I had

    <item parent="9463" 
    "Mailto:xyz@3.com"
    index="9476">
        <block lx="2.34999752" ly="6.58951187"
    
    "Mailto:xyz@3.com"
     lz="16.3749924" ux="2.39999676" uy="6.63951397" uz="16.8749962" index="151"
    material="3" look="0" up="3" color="ffcd0000"/>
    "Mailto:xyz@3.com"
    
    </item>
    
    "Mailto:xyz@3.com"
    <item parent="8956" index="2458"> 
    "Mailto:xyz@3.com"
    
    <block lx="2.34999752" ly="6.58951187"
    lz="16.3749924" ux="2.39999676" uy="6.63951397"
    uz="16.8749962" 
    "Mailto:xyz@3.com"
     index="200" material="3" look="0"
    up="3" color="abcdef"/>
    </item>
    

    You will see even on the line that has 2 occurrences of the text, they are all on their own lines.
    Now use the Mark function and type Mailto in the find what field and have search mode set to normal. Have bookmark line ticked and press “Mark All” button. Next select Bookmark (under Search main menu) and remove unmarked lines.

    You will be left with what you seek.

    Terry



  • @Terry-R said:

    (?i-s)(“Mailto:[^”]+")

    @Scott-Piker
    I made a small error in the above regex, although it doesn’t really matter. Note the first portion (?i-s). The i in that position means case-insensitive, which means the M in Mailto was disregard as a capital and is used instead as a m.

    What I really meant to do was (?-is), in this position the M is used as a capital letter, every letter is case sensitive. Sometimes use of this can help eliminate other words (or text strings) where the text you want may be included within something else but in a different case (lower instead of upper).

    The 's is there to signify the regex should treat each line separately. That overrides the ticking of a box in the Replace function called . matches newline. The 'i overrides the other box called Match case. This allows us (as providers of solutions) to set up the environment as we need it in order that our regex works as expected.

    I don’t often provide this, and indeed for the most part it doesn’t cause any issues, however it is good programming to first setup the environment as best we can. @guy038 does this (almost) religiously and I do try to emulate that thinking.

    Terry

    PS if you want to understand the whole regex use
    https://regex101.com/
    and insert my regex. It provides a good description of what it is doing.



  • That’s awesome!! Thank you sooo much!!


Log in to reply