0

I'm looking for a regular expression that should detect a complete <IMG ... random Parameter > block in a large document while the ". Matches newline" option is activated in the Find/Replace function.

Example:

I'm looking for the <IMG ... > block that contains Goal.gif. I'm currently using

(<img.+Goal.gif".+>)

but with this expression, it matches the entire block. My idea is to check for a following IMG-Tag as a negation, but I don't know how to design such a statement.

<IMG id=NoPrint border=0 hspace=0 alt="" src="Directory\Tastatur.gif" align=baseline>
<IMG id=NoPrint border=0 hspace=0 alt="" src="Directory\Tastatur.gif" align=baseline>
<IMG id=NoPrint border=0 hspace=0 alt="" src="Directory\Goal.gif" align=baseline>

What regex can I use to match only the img tag that includes Goal.gif?

  • 2
    I'm not sure why you have ticked . matches newline it is the opposite of what you want, unless some of your individual <img> tags are split across more than one line. – James P Dec 12 '16 at 15:11
  • 1
    Yes you are right, its because some blocks are split across many lines. My Goal ist to replace the complete img Statements in many html documents when they contain specific Images. – user2961606 Dec 12 '16 at 15:17
2

Here's one I tested on your sample data in Notepad++.

<img[^>]+goal\.gif.*?>

The [^>]+ works like .+ but will not match any expressions including >. This will ensure the <img and goal.gif are within the same tag.

The gif.*? makes the match non-greedy, so it will only reach to the first instance of > following gif.

I tested this on the case where there is a newline between the tag start and src attribute, and it still worked.

| improve this answer | |
  • Thank you, this works. But can you tell me what the "\" does before the .gif tag ? Works with and without it anyway. Is it just to prevent errors cause of the dot ? – user2961606 Dec 13 '16 at 6:54
  • 1
    The slash escapes the regular expression interpretation of the period. That way, it only matches a period character and not a wildcard. Without escaping it, the pattern would match img tags that include goalAgif, goal1gif, etc. – Excellll Dec 13 '16 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.