Search code examples
regexnotepad++

Replace a string using Notepad++ and regex


I have strings like this:

<img src="http://www.example.com/app_res/emoji/1F60A.png" /><img src="http://www.example.com/app_res/emoji/1F389.png" />
<img src="http://www.example.com/app_res/emoji/1F61E.png" /><img src="http://www.example.com/app_res/emoji/1F339.png" />

I want them to be like this:

&#x1F60A; &#x1F389;
&#x1F61E; &#x1F339;

In Notepad++, I tried this :

Find what: ^\s*<img src="http://www.example.com/app_res/emoji/(1F.*).png" />

Replace with: &#x\1;

The result is not as expected:

&#x1F60A.png" /><img src="http://www.example.com/app_res/emoji/1F389;

How to best isolate the regular expression ?

Any help is welcome ! Thank you


Solution

  • You're using the unspecific . together with the greedy star *. Don't do that here, as this tends to overshoot the target.

    Be more specific.

    The file name (in your case) does not contain dot's. Let's use "anything except a dot" ([^.]*) instead of "anything" (.*):

    ^\s*<img src="http://www.example.com/app_res/emoji/(1F[^.]*).png" />