Community
    • Login

    Paste a new line of code after every instance of an existing line

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 2 Posters 1.1k 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.
    • berrmirtB
      berrmirt
      last edited by

      That’s pretty much it…

      I have a line that is:
      <span>old text here</span>

      And I want to paste a new line:
      <span>old text here</span>

      After each occurrence of the old line.

      1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones
        last edited by

        @berrmirt ,

        Your example shows adding the same text after each occurrence of the old. I am not sure that it what you meant.

        Assuming for the moment that your original text is:

        <span>old text here</span>
        <span>something else</span>
        <span>old text here</span>
        <span>something else</span>
        <span>old text here</span>
        <span>something else</span>
        <span>old text here</span>
        <span>something else</span>
        <span>old text here</span>
        <span>something else</span>
        

        If you use

        • find = <span>old text here</span>(\R)
        • replace = $0<span>new text here</span>$1
        • search mode = regular expression

        it will become:

        <span>old text here</span>
        <span>new text here</span>
        <span>something else</span>
        <span>old text here</span>
        <span>new text here</span>
        <span>something else</span>
        <span>old text here</span>
        <span>new text here</span>
        <span>something else</span>
        <span>old text here</span>
        <span>new text here</span>
        <span>something else</span>
        <span>old text here</span>
        <span>new text here</span>
        <span>something else</span>
        

        Is that what you wanted?

        -----
        explanation:

        • in the find, most is literal text, except (\R), which says “match any newline (CR, LF, or CRLF), and save that end-of-line-type into $1`”
        • in the replace:
          • $0 = the whole matched line from FIND, including newline
          • then the new plaintext
          • $1 = make a copy of the newline matched, so that this new line will match the same EOL-type as the matched line

        -----
        FYI: if you have further regex (search-and-replaceneeds, study this FAQ and the documentation it points to. Before asking a new regex question, understand that for future requests, many of us will expect you to show what data you have (exactly), what data you want (exactly), what regex you already tried (to show that you’re showing effort), why you thought that regex would work (to prove it wasn’t just something randomly typed), and what data you’re getting with an explanation of why that result is wrong. When you show that effort, you’ll see us bend over backward to get things working for you. If you need help formatting the data so that the forum doesn’t mangle it (so that it shows “exactly”, as I said earlier), see this help-with-markdown post, where @Scott-Sumner gives a great summary of how to use Markdown for this forum’s needs.
        Please note that for all “regex” queries – or queries where you want help “matching” or “marking” or “bookmarking” a certain pattern, which amounts to the same thing – it is best if you are explicit about what needs to match, and what shouldn’t match, and have multiple examples of both in your example dataset. Often, what shouldn’t match helps define the regular expression as much or more than what should match.

        1 Reply Last reply Reply Quote 3
        • berrmirtB
          berrmirt
          last edited by

          Thank you, sir, that’s exactly what I was looking for!

          Also, I appreciate the detailed explanation, it was very helpful.

          Further, I was able to use your link to determine that I needed to add "(\t\t\t) to my replace expression to align the ‘new’ line with the old code line.

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