Extension:Regex Fun

Regex Fun provides mediawiki with four new parser functions for performing regular expression handling within wiki pages. The main difference to other regex extensions such as RegexFunctions or RegexParserFunctions, besides richer functionality, is that this extension provides a function  for encoding user-provided strings for the use within a regular expression, a function   to access subexpression results, and it introduces mediawiki-related regular expression modifiers   and changes the meaning of the   modifier in a meaningful way.

Usage
This section will introduce each of the four regular expression related functions which come along with this extension.

#regex
This function allows simple search via regular expression. It will return the first match within a string. It also has a replacement mode where each match of the expression is being replaced (up to an optional limit). It allows the use of php preg modifiers, in replace mode it changes the meaning of the  modifier and introduces the   modifier.


 * syntax:
 * search: E.g. " " would return " "
 * replace: E.g. " " would return " " Parameter limit is optional (default  ) Parameter replacement allows back-references " ", where n is a number. " " is possible as well but not recommended.
 * replace: E.g. " " would return " " Parameter limit is optional (default  ) Parameter replacement allows back-references " ", where n is a number. " " is possible as well but not recommended.


 * MW modifiers (flags):  can use all php preg regex modifiers with their exact meaning, except for the   modifier which would be a security risk. The   modifier has another meaning instead in repacement mode. In addition, the   modifier is introduced.
 * : Only in replacement mode. If set, the function will return an empty string if no replacement could been done (if the expression didn't match anything). Without the flag the function would simply return the input string.
 * : Only in replacement mode. Before the replacement of matching strings is done, references in the replacement string (such as  or  ) will be replaced by their matches. In case   is set, after this replacement the replacement string will first be parsed before being inserted in the original string. This allows to use parser functions or templates within the replacement string which will run over the references first. Example: "  " would return " " (considering Template:(( and Template:)) exist within the wiki)
 * : Only in replacement mode. Before the replacement of matching strings is done, references in the replacement string (such as  or  ) will be replaced by their matches. In case   is set, after this replacement the replacement string will first be parsed before being inserted in the original string. This allows to use parser functions or templates within the replacement string which will run over the references first. Example: "  " would return " " (considering Template:(( and Template:)) exist within the wiki)

#regexall
Searches the whole string for as many matches as possible and returns them separated by a separator. Also gives control from which match to start and where to end. This function can be particularly useful together with extensions ArrayExtension and HashTables.


 * syntax: E.g. " " would return " ".


 * Optional parameters
 * separator (default " ")
 * offset (default ) If non-negative, first item will come from that offset. If negative, the first item comes that far from the end of all items.
 * length (default " function. Subexpressions basically are the parts within parentheses " " which can be referenced to as " " (where n is a number) within   in replacement mode. It is possible to set a certain index in parameter 1 or to use a whole string, containing references, following the rules of the   replacement string.


 * syntax:
 * using specific index: E.g.
 * using reference string: E.g.
 * Parameter default can contain a string which will be used in case the given index dosn't exist, the last use of  failed or   hasn't been called yet.
 * Parameter default can contain a string which will be used in case the given index dosn't exist, the last use of  failed or   hasn't been called yet.


 * rules: There are a few points you should be aware of, using this function:
 * default will be used in case  wasn't executed before or the last execution caused an error.
 * If there lays a template call between the last  use and   is being called from within that template,   will consider the function call from within the template as refernce. This will lead to confusing outputs. This might be fixed in a later version.

#regexquote
Important function to escape user provided data which should be used as part of a regular expression. User provided input, for example template parameter provided data, should always run through this funcdtion first to make sure that special characters like " " or " " in the user input won't irritate the regular expression. Technically, this function will run the php function [preg_quote] over the string and in case the first character has a special meaning in MW, it will be replaced it with its hexadecimal notation e.g. " " instead of " " (to prevent the line from becoming a MW list).


 * syntax: Parameter delimiter is optional and should be the character used as delimiter within the regular expression where the text should be used. By default, the delimiter is set to " " since it is the most common delimiter in most examples.


 * example: This will highlight some item provided by template parameter Favorite within a list of items, separated by comma, provided by parameter Items. If   wasn't used here and Favorite would contain some special character, this would break the whole expression and return an error message!

invalid regular expression handling
Instead of outputting a php notice in the event of an invalid regular expression, this will output an inline wiki error message which can be caught by extension ParserFunctions error catching #iferror function.

Installation
Once you have downloaded the code, place the RegexFun directory within your MediaWiki extensions directory. Then add the following code near the bottom of your LocalSettings.php file:

Configuration
Regex Fun comes with two global customization variables. Their default values can be changed by including them into localsettings.php after the inclusion of RegexFun.php.


 * $egRegexFunDisabledFunctions : Array which allows to define functions which should not be available for use within the wiki. For example if you want to prevent users from using  and , simply set this to:


 * $egRegexFunMaxRegexPerParse : Defines the maximum regular expression executions per ongoing parser process. This counts all major executed regular expression usages triggered by this extension. The counter will be increased by '#regex', '#regexall' and by '#regex_var' in case a reference string is given but not if only an index is requested. '#regexquote' is not affected. Before the limit is exceeded, a  catchable error message will be put out instead of the result of the called function. By default the limit is set to   which disables any limitation.
 * Note: Instead of using a limit per page, this limit is per  process bound to each Parser object. This makes sense to avoid complications on page import or when the job queue is updating pages because a single increasing global counter would not really be per page but rather per session then.