-4

Notepad ++ (REGEX); Invert Selection,

The following 2 different codes, it works very nicely. :) now I want to combine these two different code!

\bhttps?:[^)''"\s]+\.(?:jpg|jpeg|gif|png) 

https://codereview.stackexchange.com/questions/20126/regex-to-get-all-image-links

^((?!hello).)*$  

notepad++ Inverse Regex replace (all but string)

Community
  • 1
  • 1
Eren SAĞLAM
  • 11
  • 1
  • 4

1 Answers1

1

Forward

Ensure you're using the latest version of notepad++, there where known problems using regex in notepad++ v5 and before which have been corrected in v6.

Description

  • Capture the src attribute value
  • works with double quoted, single quoted, and non-quoted attribute values
  • avoids tricky edge cases which normally trip up simple expressions

<img(?=\s|>)(?=(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*?\ssrc=(['"]?)(.*?)\1(?:\s|>))(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*>

enter image description here

How To

From Notepad++

  1. press the ctrlF to enter the find and replace mode

  2. Select the Regular Expression option

  3. Select the ". matches newline" option

  4. In the "Find what" field place the following regex <img(?=\s|>)(?=(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*?\ssrc=(['"]?)(.*?)\1(?:\s|>))(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*>

  5. Click Find All

Regex Example

Live Demo

Sample Text

Note the first two image tags have some very difficult edge cases

<img onmouseover=' img = 10; src="NotYourImage.png" ; if (3 <img && src="NotYourImage.png" && 6>3) { funRotate(src) ; } ; ' src="ImageYouAreLookingFor.png">
<img onmouseover=' src="NotTheDroidsYouAreLookingFor.png" ; if (x > 3) { funRotate(src); } ' src="http://another.example/picture.png">
<img src="./CaptchaServlet?rd=htb54m" class="flt" id="captcha" height="33" width="110"/>

Matches

Group 0 gets the entire image tag
Group 1 gets the quote used to surround the src attribute, and is used to ensure the correct closing quote is matched
Group 2 gets the src value, or if you use the alternate regex above, will receive just the rd query string

[0][0] = <img onmouseover=' img = 10; src="NotYourImage.png" ; if (3 <img && src="NotYourImage.png" && 6>3) { funRotate(src) ; } ; ' src="ImageYouAreLookingFor.png">
[0][1] = "
[0][2] = ImageYouAreLookingFor.png

[1][0] = <img onmouseover=' src="NotTheDroidsYouAreLookingFor.png" ; if (x > 3) { funRotate(src); } ' src="http://another.example/picture.png">
[1][1] = "
[1][2] = http://another.example/picture.png

[2][0] = <img src="./CaptchaServlet?rd=htb54m" class="flt" id="captcha" height="33" width="110"/>
[2][1] = "
[2][2] = ./CaptchaServlet?rd=htb54m
Community
  • 1
  • 1
Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43