I'm trying to use a regex to insert a template into a page, before all category or interwiki links, but after everything else. So if you have a page that ends like this:
== See Also ==
* [[Link one]]
* [[more link]]
* [//external.link external link]
[[Category:Pages]]
[[de:Spezial Page]]
I want the template {{template}}
to be inserted before the [[Category:Pages]]
but after everything else.
Note: The last section is not necessarily a list - it could be
== References ==
<references/>
or even something else. The point is to insert it before all category/interwiki links at the end, but after the last section.
What regex can help me do this? I've tried (?P<pre>[\s\S]+)(?P<cats>(?:\[\[[^]]:[^]]\]\])*$)
as the matching expression with \g<pre>{{template}}\n\g<cats>
as the substituting expression, but that simply inserts it at the very end.
Regex flavor: Python 2