Regex for MediaWiki wikitext templates

2019-08-13 03:39发布

问题:

Yaron Koren's ReplaceText MediaWiki extenion allows me to use regular expressions to replace text in MediaWiki. I have about 300 MediaWiki pages that have two instances of a template. I want to delete only the second template using regular expressions. (replace it with nothing)

I have:

}} <--end of first template-->

{{Template Call

|blahField=blahValue

|blahField=blahValue

|...

}}

I think I want to search for: ^[ \\}} \r \\{{ *? ]

and replace it with: ^[ \\}} ]

but this is not producing the result I want when I test it at rexv.org

Appreciate some help with regular expressions.

回答1:

On the extension page it says

The set of regular expressions allowed is basically a small subset of the PHP and MySQL regular-expression sets (it has only been tested with MySQL - whether it works on other database systems is unknown). The characters that one can use in the search string are "( ) . * + ? [ ] |"

So maybe the characters you are using don't work. Is it possible the \ is messing it up?



回答2:

ReplaceText is not suitable for such replacements. Use PyWikipediaBot or AutoWikiBrowser.



回答3:

I have some hack here:

  1. Make your {{Template Call}} Template to empty.
  2. Use ReplaceText to Replace \{\{ *Template[ _]Call (with regex to avoid badly-formatted calls) to {{subst:Template Call.
  3. Then the places you have Template Call will be replace by contents of {{Template Call}}, which is empty.

Hope it helps.