简体   繁体   中英

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

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" />

You may try the following find and replace, in regex mode:

Find:    <img src=".*?/([A-Z0-9]+\.\w+"\s*/><img src=".*?/([A-Z0-9]+\.\w+"\s*/>
Replace: &#x$1; &#x$2;

Here is a working regex demo .

Try

Find: ^<.*?/(1\w+).*?/(1\w+).* Replace: &#x$1; &#x$2; &#x$1; &#x$2;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM