Search code examples
regexreplacenotepad++

Find and replace in multple lines (URLs of different sizes), up to the last slash, while keeping the file names


I've been searching and nothing seems to be an exact answer to my problem, some are different or just incomplete solutions, as they are wrapping around the whole file on search and merging links altogether. Basically I need to replace everything in a link path, except for the filname with pdf extension( and stop there, it shouldn't target consecutive links ie into just one) Thanks in advance!

Links of different path sizes to be targeted: <a href="**/images/2023/urbanism/rcs/**filename1.pdf">filname1</a> <a href="**/images/2023/urbanism/sector1/rcs/**filename2.pdf">filname2</a>

Links after replace was made:

<a href="/wp-content/uploads/2024/02/filename1.pdf">filname1</a> <a href="/wp-content/uploads/2024/02/filename2.pdf">filname2</a>

For targeting, I tried using href="/.?/.?.pdf", and for replacing I used href="/wp-content/uploads/2024/02/.*?.pdf" But it doesn't work (anymore). I remember using this method few months ago and it used to work.


Solution

  • You can use

    \bhref="/[^"]*/([^"/]*\.pdf)"
    

    Replace with href="/wp-content/uploads/2024/02/$1".

    Details

    • \b - word boundary
    • href="/ - literal text
    • [^"]* - zero or more chars other than "
    • / - a / char
    • ([^"/]*\.pdf) - Group 1 (referred to as $1 in the replacement pattern): zero or more chars other than " and / and then .pdf
    • " - a " char.

    Note that a literal dot must be escaped.