At the end of my previous post, I said :
If you don’t want to repeat the word Fred, several times in the search regex,…
and I gave a second, but rather complicated, regex !
Thinking about it, here is a third and more simple regex, for the same purpose :
SEARCH (?-s)<p[^<>\r\n]*(\K class="(fred)"| class=".+\K (?2)| class="\K(?2) )
REPLACE Leave EMPTY
Remember that the (?n) syntax, called a subroutine call, represents the regex enclosed in the nth pair of round brackets, when going through the regex, from left to right and can be placed after of before the group to which it refers !
In our example, the regex is, simply, the string fred and the (?2) forms are located after the second group (fred)
It’s very important to note that the similar regex, which uses back-references, does not work ! Indeed, the regex :
(?-s)<p[^<>\r\n]*(\K class="(fred)"| class=".+\K \2| class="\K\2 ), matches, only, the two lines, below :
<p class="fred">some text</p>
<p id="12345" class="fred">some text</p>
Why ? Well, these positive matchs concern the first case of the alternative. When it tries to match the two other cases of the alternative ( " class=".+\K \2" or " class="\K\2 " ) the group 2 does not exist, as the first part of the alternative is not used !!