Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace with Regex in Notepad++

I'm trying to replace the part the text with the same text and some extra, example:

Initial text

<href="../doc/d5807346.pdf" class="document">3.2.1.&nbsp;&nbsp;&nbsp;&nbsp;EXAMPLE</a></div><div style="clear:both;"></div>

After

<href="../doc/d5807346.pdf" class="document" download="3.2.1.&nbsp;&nbsp;&nbsp;&nbsp;EXAMPLE">3.2.1.&nbsp;&nbsp;&nbsp;&nbsp;EXAMPLE</a></div><div style="clear:both;"></div>

I'm using in Notepad++ the following Regex:

  • Find: ((?<=class="document">).*?(?=<))
  • Replace with: download="\1">\1

End result is not what I'm expecting, note the > between class="document" and download :

<href="../doc/d5807346.pdf" class="document"> download="3.2.1.&nbsp;&nbsp;&nbsp;&nbsp;EXAMPLE">3.2.1.&nbsp;&nbsp;&nbsp;&nbsp;EXAMPLE</a></div><div style="clear:both;"></div>

I'm stuck on trying to figure out how to prevent that.

like image 501
Juanjo Avatar asked Jul 13 '26 03:07

Juanjo


2 Answers

  • Ctrl+H
  • Find what: (class="document")>([^<]+)
  • Replace with: $1 download="$2">$2
  • CHECK Wrap around
  • CHECK Regular expression
  • Replace all

Explanation:

(class="document")      # group 1
>                       # literally >
([^<]+)                 # group 2, 1 or more any character that is not "<"

Replacement:

$1              # content of group 1 + a space
download="      # literally
$2              # content of group 2
">              # literally
$2              # content f group 2

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

like image 90
Toto Avatar answered Jul 15 '26 15:07

Toto


Try this:

  1. Find: (?:class="document">)([^<]*)
  2. Replace (corrected): class="document">download="$1">$1

Tested in N++

Before: <href="../doc/d5807346.pdf" class="document">3.2.1.&nbsp;&nbsp;&nbsp;&nbsp;EXAMPLE</a></div><div style="clear:both;"></div>

After: <href="../doc/d5807346.pdf" class="document">download="3.2.1.&nbsp;&nbsp;&nbsp;&nbsp;EXAMPLE">3.2.1.&nbsp;&nbsp;&nbsp;&nbsp;EXAMPLE</a></div><div style="clear:both;"></div>

It's helpful to Use "online Regex tester" for visual debug regular expression

like image 25
Алексей Р Avatar answered Jul 15 '26 15:07

Алексей Р