Extension:RegexFunctions

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
RegexFunctions

Release status: stable

Implementation Parser function
Description Regular expression based parser functions
Author(s) Ryan Schmidt (SkizzerzTalk)
Last version 1.4.1
MediaWiki 1.13+
License None (Public Domain)
Download Download snapshot
Subversion [Help]

Browse source code
View code changes

Usage
Changelog

Parameters

$wgRegexFunctionsPerPage, $wgRegexFunctionsAllowModifiers, $wgRegexFunctionsAllowE, $wgRegexFunctionsAllowOptions, $wgRegexFunctionsLimit, $wgRegexFunctionsDisable

Check usage (experimental)

Contents

[edit] Usage

As of now, this extension defines three parser functions, rmatch, rsplit, and rreplace

rmatch
{{#rmatch:string|pattern|return|notfound|offset}}
string (required): The text the function uses for the matching.
pattern (required): The regular expression to use on the text.
return (required): What to return if the match was successful. $# and \# contain the captured text, and $$# and \\# contain the offsets of that text. Use ${#}# or \{#}# if you want a group followed by another number.
notfound (optional): What to return if the match was not successful. Defaults to an empty string.
offset (optional): The offset in bytes to start the match at. Defaults to 0 (the beginning of the text).
rsplit
{{#rsplit:string|pattern|piece}}
string (required): The text the function will split.
pattern (required): The regular expression to use on the text. No text is captured.
piece (optional): The piece to return. Defaults to 0 (the first piece).
rreplace
{{#rreplace:string|pattern|replace}}
string (required): The text the function will replace.
pattern (required): The regular expression to use on the text.
replace (required): The text to replace the matched text with. Both $# and \# will call the capturing groups, use ${#}# or \{#}# if you want a group followed by another number.

[edit] Pattern help

For help on constructing the syntax, please read [1] for general help and [2] for an overview of the modifiers.

[edit] Installation

Please download the correct version of RegexFunctions for your MediaWiki version from the extension distributor and unpack in your extensions directory. Refer to the following chart to determine which version to download:

MediaWiki version Version from the extension distributor you should download
1.12 and below RegexFunctions is incompatible with your MediaWiki version. Please upgrade MediaWiki.
1.13 to 1.15 Download the "1.15.x" version (RegexFunctions version 1.3)
1.16+ Download the "Development version (trunk)" version (RegexFunctions version 1.4.1)

The "1.13.x" version corresponds to RegexFunctions version 1.2, and "1.14.x" corresponds to version 1.3. See the changelog for more information on what each version encompasses.

Once unpacked, add the following to the bottom of LocalSettings.php:

require_once("$IP/extensions/RegexFunctions/RegexFunctions.php");

[edit] Configuration parameters

$wgRegexFunctionsPerPage
How many functions are allowed per page. Default is 10, but it is recommended that it is set above 3 for usability
$wgRegexFunctionsAllowModifiers
Allows modifiers (e.g./i for case insensitivity). Does not enable the /e modifier in the rreplace function, see the next variable for that. Defaults to true.
$wgRegexFunctionsAllowE
Allows the /e modifier in rreplace, which treats the 'replace' text as PHP code. Don't enable this unless you trust all of your users, as it opens up XSS vectors and other security concerns. Defaults to false.
$wgRegexFunctionsAllowOptions
Allows internal options (e.g. (?U) for ungreedy) inside of the regex itself. Defaults to true.
$wgRegexFunctionsLimit
Limit for the number of pieces in the rsplit function and replaces in the rreplace function. -1 is unlimited. Defaults to -1.
$wgRegexFunctionsDisable
Array of functions you want to disable. Defaults to an empty array.

[edit] Changelog

Version 1.4.1
Fixed issue where disabling rmatch disabled every function, and it was otherwise impossible to disable rsplit and rreplace.
Version 1.4
Moved to new message declaration style, nothing changed frontend-wise
Version 1.3
Sanitation process should no longer misinterpret things as internal options.
Version 1.2
Pattern goes through a sanitation process before being passed onto one of the preg_* functions.
Version 1.1
Localization updates
Version 1.0
Initial Release, defines three functions: rmatch, rsplit, and rreplace

[edit] See also

  • ReplaceSet - an excellent substitute for using nested #replace commands when you need to perform a sequence of replaces on a single text string.
  • StringFunctions - some helpful string operations.
  • Extension:Regex Fun - just another regex extension with more functionality but without any customization variables for limitations.
Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Print/export
Toolbox