简体   繁体   中英

Regex in Notepad++ for replacement

i've search for that info, but can't to figure out how to understand properly a regular expression. I have a html file and there are multiple <img> tags. Each of these tags have attribe src with following data: /newsletter/images/light/b/NUMERICVALUE/IMAGENAME.jpg

Assuming that the NUMERICVALUE and the IMAGENAME are always different value in each src attribute.

What i need is to remove /NUMERICVALUE from each url. How i suppose to do that ?

Thanks for any suggestion.

Here are a list of examples:

/newsletter/images/light/b/617359405/lf-header.jpg /newsletter/images/light/b/617359406/up-logo.jpg /newsletter/images/light/b/617359407/logo-header.jpg

Search: src=\\"\\/newsletter\\/images\\/light\\/b\\/[^\\/]*\\/([^\\"]*)\\"
Replace: src="/newsletter/images/light/b/\\1"

Explanation:

  • Defines the full URI: \\/newsletter\\/images\\/light\\/b\\/[^\\/]*\\/([^\\"]*)
  • This will find anything WITHOUT a / slash [^\\/]*
  • This will find anything WITHOUT a " quote ([^\\"]*)
  • The ( and ) tells the regular expression engine to capture the content of it
  • In the replacement \\1 forces the regular expression engine to insert capture 1 at this place

Edit : Updated to be used on all src= attributes as mentioned in the comments.

I guess NUMERICVALUE is an actual numeric value, so a simple solution:

Find what: /newsletter/images/light/b/\d+/([a-zA-Z0-9_]+).jpg
Replace with: /newsletter/images/light/b/\1.jpg

change contents of [a-zA-Z0-9_] to what you need.

This barely programming-related but anyway:

Find what: (<img src=.*/)\d+/
Replace with: \1

Input:

<img src="/newsletter/images/light/b/654/IMAGENAME.jpg"/>
<img src="/newsletter/images/light/b/5752/IMAGENAME.jpg"/>
<img src="/newsletter/images/light/b/78697345/IMAGENAME.jpg"/>
<img src="/newsletter/images/light/b/7896789/IMAGENAME.jpg"/>
<img src="/newsletter/images/light/b/45/IMAGENAME.jpg"/>
<img src="/newsletter/images/light/b/8/IMAGENAME.jpg"/>
<img src="/newsletter/images/light/b/4567837/IMAGENAME.jpg"/>

Output:

<img src="/newsletter/images/light/b/IMAGENAME.jpg"/>
<img src="/newsletter/images/light/b/IMAGENAME.jpg"/>
<img src="/newsletter/images/light/b/IMAGENAME.jpg"/>
<img src="/newsletter/images/light/b/IMAGENAME.jpg"/>
<img src="/newsletter/images/light/b/IMAGENAME.jpg"/>
<img src="/newsletter/images/light/b/IMAGENAME.jpg"/>
<img src="/newsletter/images/light/b/IMAGENAME.jpg"/>

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