正規表現を使用して HTML を評価するのは少し危険ですが、欠点を受け入れる意思がある場合*、肯定的な先読みアサーションを使用してこれを機能させることができます。
regex = re.compile(r'<img (?=[^>]*\balt=")(?=[^>]*\bsrc=")(?=[^>]*\bclass=")')
現在の文字列に<img
(同じタグ内で) alt="
、src="
およびclass="
が任意の順序で続く場合に一致します。
説明:
<img # Match '<img'
(?= # Assert that it's possible to match the following from this position:
[^>]* # Any number of characters except >
\b # A word boundary (here: start of a word)
alt=" # The literal text 'alt="'
) # End of lookahead
(?=[^>]*\bsrc=") # Do the same for `src`, from the same position as before
(?=[^>]*\bclass=") # Do the same for `class`, from the same position as before
*もちろん、この正規表現は、一致するタグがコメント内にあるか、コメントによって中断されているか、不正な形式であるか、<pre>
タグで囲まれているか、または実際の HTML パーサーの意味を変更する可能性のあるその他の状況については完全に無知です。