Search code examples
regexnotepad++regular-language

How to write RegEx which starts and ends on different lines, these lines include one matching word only inside


I want to replace a code group of 3 lines on the same file and on other fi les by using notepad++ find & replace feature with regular expressions (regex) Only thing in common on these lines which i want to select is they include the same word "mailicon4.gif" on the last line, and i want to select all the last line, and i want that the selection starts with "a class=" tag which comes 2-3 lines earlier. I copied the part i want to select with find and replace with "" to delete it.

I already tried such a code for example: ?maili.?$\R

It selects the last paragraph but doesn't start with the last "

    <a class="nonblock nontext" id="u209382" href="mailto:[email protected]">
      <!-- rasterized frame --><img class="temp_no_img_src" id="u209382_img" alt="" width="66" height="66"
        data-orig-src="images/mailicon4-u209382.png?crc=143036675" src="images/mailicon4.gif?crc=4208392903" /></a>

Solution

    • Ctrl+F
    • Find what: <a (?:(?!</a>).)*mailicon4\.gif.*?</a>
    • Replace with: $1 # a space then $1
    • check Wrap around
    • check Regular expression
    • CHECK . matches newline

    Explanation:

    <a                  # start tag
    (?:(?!</a>).)*      # 0 or more any character, but not </a>
    mailicon4\.gif      # literally
    .*?                 # 0 or more any character, not greedy
    </a>                # literally
    

    Demo