Search code examples
pythonregexpython-2.7mediawiki-templates

Regex to match MediaWiki template without certain named parameter


I’ll get to the point: I need a regex that matches any template out of a list that have a date parameter - so assuming that my (singleton for now) list of templates is “stub”, the things below that are in bold should be matched:

  • {{stub}}
  • {{stub|param}}
  • {{stub|date=a}}
  • {{stub|param|date=a}}
  • {{stub|date=a|param}}
  • {{stub|param|date=a|param}} Note: “param” means any number of parameters there.

Additionally, it would be nice if it could also match if the date parameter is blank, but this is not required.

The current regex I have so far is

{{((?:stub|inaccurate)(?!(?:\|.*?\|)*?\|date=.*?(?:\|.*?)*?)(?:\|.*?)*?)}}

However it matches the fourth and sixth items in the list above.

Note: (?:stub|inaccurate) is just to make sure the template is either a stub or inaccurate template.

Note 2: the flavor of regex here is Python 2.7 module RE.


Solution

  • I think it's enough to have a negative look-ahead, which tries to match date at any position?

    {{((?:stub|inaccurate)(?!.*\|date=).*)}}

    If empty date parameters have a | following the equals sign, then use

    {{((?:stub|inaccurate)(?!.*\|date=[^|}]).*)}}