@Paul-Wormer
Time to unpack the (?s-i:(?!ESR).)*? part of the regex, which is what your new version is currently missing.
Essentially this is divided into four parts:
the flags: ?s-i
These say that:
the . metacharacter should match everything (that’s the s flag)
we want to be case-sensitive. That’s the -i part.
consume a character unless the end of the search region is right ahead: (?!ESR).
This looks ahead without consuming any characters to see if the ESR is in front of you, and then stops if it is. An example:
Search string: cacb ab
regex: (?!ab).
At the start of the string, we look ahead for ab. The next character is c, so we consume c. (remember, this is SUCCESS because we want to NOT match the ESR).
string: cacb ab
want: _ab
match: !
consumed: YES
Now we’re after the first c, before the first a. We look ahead for ab, but see ac instead, so we’re clear to advance.
string: cacb ab
want: __ab
match: *!
consumed: YES
You can see that there are no ab anywhere except at the end of the string, so everything will match.
Let’s fast-forward to the end of the string:
string: cacb ab
want: ______ab
match: **
consumed: NO
We’re now positioned between the blankspace and the ending ab. The next two characters are ab, so this whitespace character will NOT be matched.
Do the above thing any number of times: (?s-i:(?!ESR).)*?
This just says to keep looking ahead and stopping if the ESR is ahead, then consuming a character, then looking ahead… until the ESR is reached or the entire string is consumed.
Interesting note: Rexegg.com refers to this as “tempered greed” because you’re greedily trying to eat the whole string, but checking to see if you’re full before you take each bite.
Putting it all together:
So as @guy038 illustrated above, the (?-i:<text>|(?!\A)\G)(?s-i:(?!</text>).)*?\K-+ regular expression is going to start with (?-i:<text>|(?!\A)\G) by matching either <text> (the BSR) or the end of the last matched region (unless you wrapped around).
Now the (?s-i:(?!</text>).)*? part comes into play. It behaves as I described above: the negative lookahead for </text> ensures that you cannot go past the ESR.
The rest is the same, as you correctly identified:
forget everything you matched so far (the \K)
match any number of - characters (-+).
For the record, I think that part of the problem with the readability of this regex has to do with the flags. The version of the regex without flags, (?:<text>|(?!\A)\G)(?:(?!</text>).)*?\K-+, is I think a bit less confusing.