Thread:OH&S/@comment-5275759-20200101001032/@comment-25364614-20200314010248

Alright, I think I've got the regex working.

Scenario: Say you want to check if Section H2 ( ==H2== ) directly follows Section H1 ( ==H1== ) in an article.

Further Requirements:
 * H1 and H2 are on the same level ( ==H#== )
 * There is no Section H3 ( ==H3== ) between them.
 * Sections can have subsections as well: Subsection H1A ( ===H1A=== )
 * Section text may have references in them.

The regex to capture all of this information is as follows:

(?<!=)==H1==(?!=)(?:(?:(?!==)|(?=={3,})|(?<==)(?===))[\s\S])+(?<!=)==H2==(?!=)

This looks complicated so I'll break it down:
 * (?<!=)==H1==(?!=) - This looks for the Section H1 wikitext while making sure its only at the == level and not anything lower like a subsection level ===.
 * (?:(?:(?!==)|(?=={3,})|(?<==)(?===))[\s\S])+ - After the matching the section header, this code looks for all the text afterwards such that it:
 * includes the = sign in references
 * excludes if it finds a section heading ==.
 * includes if it finds a subsection heading ===, ====, etc.
 * (?<!=)==H2==(?!=) - Same as the first point, this looks for the Section H2 wikitext to end the search string.

So it will capture this:

==H1== H1 text with references ===H1A=== H1A text with references ==H2==

but NOT this:

==H1== H1 text with references ===H1A=== H1A text with references ==H3== H3 text with references ==H2==

The regex is for the simplest case scenario of checking the order of Level 2 Headings. It will need to be adjusted for other cases.