Search code examples
regexnotepad++href

Regex in Notepad++ to move contents of an element to an attribute value


I'm trying to solve a regex riddle. Let's say I have rows of hrefs looking like this:

<a href="http://domain.com/5et46ffd5e3wR23">anchor1.in</a>
<a href="http://domain.com/6utr4dGHJ6wFL">an3.php</a>
<a href="http://domain.com/k543sk6k3F6hJ">setup.exe</a>

What I want the regex (or any other solution) to do is to take the href title and copy it over to the actual url with a foward slash in front of it.

A successful result would become:

<a href="http://domain.com/5et46ffd5e3wR23/anchor1.in">anchor1.in</a>
<a href="http://domain.com/6utr4dGHJ6wFL/an3.php">an3.php</a>
<a href="http://domain.com/k543sk6k3F6hJ/setup.exe">setup.exe</a>

If you can solve this please explain how you did it.


Solution

  • You can use the following to match:

    (<a\s+href=")(.*?)(">)(.*?)(<\/a>)
    

    And replace with:

    \1\2/\4\3\4\5
    

    See DEMO and Explanation