扩展:正则表达式功能

This page is a translated version of the page Extension:RegexFunctions and the translation is 30% complete.
Other languages:
English • ‎Türkçe • ‎dansk • ‎français • ‎中文 • ‎日本語
MediaWiki擴充功能手冊
OOjs UI icon advanced.svg
RegexFunctions
发布状态: 稳定版
实现 解析器函数
描述 Regular expression based parser functions
作者 Ryan Schmidt (Skizzerz讨论)
最新版本 2.0.0 (2020-03-18)
MediaWiki 1.31+
许可协议 公有領域
下载
用法
翻譯RegexFunctions擴充功能如在translatewiki.net可用
检查使用和版本矩阵。

The RegexFunctions extension provides regular expression based parser functions.

用法

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

#rmatch:
{{#rmatch:string|pattern|then|else}}
string (必需) - The text the function uses for the matching.
pattern (必需) - The regular expression to use on the text.
then (可选) - What to return if the match was successful. $# and \# contain the captured text. Use ${#}# if you want a group followed by another number.
else (可选) - What to return if the match was not successful. 默认为空字符串。
#rsplit:
{{#rsplit:string|pattern|piece}}
string (必需) - The text the function will split.
pattern (必需) - The regular expression to use on the text. No text is captured.
piece (可选) - The piece to return. Defaults to 0 (the first piece). Negative numbers return pieces starting from the end. For example, -1 is the last piece.
#rreplace:
{{#rreplace:string|pattern|replacement}}
string (必需) - The text the function will replace.
pattern (必需) - The regular expression to use on the text.
replacement (必需) - The text to replace the matched text with. $# and \# contain the captured text. Use ${#}# if you want a group followed by another number.

Except for the string parameter, every parameter can be specified either by name or position. For example, {{#rmatch:some string|pattern=^.+$|then=success|else=failure}}. If your wiki's content language is not English, both the English names as well as a translated name may be usable. In addition, every function supports the following named parameters which allow you to modify the behavior of the regex:

  • multiline - If non-empty, ^ and $ match the start and end of each line, rather than the start and end of the entire string.
  • caseless - If non-empty, the pattern is case-insensitive.
  • ungreedy - If non-empty, * and + do not match greedily whereas *? and +? match greedily.
  • extended - If non-empty, the regex can contain spaces and inline comments. See the PHP documentation in the pattern help section for more information.
  • dotall - If non-empty, . matches all characters, including newlines. Normally it does not match newlines.

For example, {{#rmatch:XxXxX|x+|$0|caseless=1}} would result in the output XxXxX because x+ matches both uppercase and lowercase X's when the caseless modifier is set.

Pattern help

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

Delimiters are not required on the pattern in general and should be omitted for readability where possible, but are required if you would like to specify the single-letter modifiers after the pattern. In this event, the delimiters must be forward slashes (no other delimiter type is supported).

安装

  • 下载文件,并将其放置在您extensions/文件夹中的RegexFunctions目录内。
  • 将下列代码放置在您的LocalSettings.php的底部:
    wfLoadExtension( 'RegexFunctions' );
    
  •   完成 – 在您的wiki上导航至Special:Version,以验证扩展已成功安装。

参见

  • 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.
  • Regex Fun - just another regex extension with more functionality but without any customization variables for limitations.
  • Extension:RegexParserFunctions