扩展:正则表达式功能
发行状态: 稳定版 |
|
|---|---|
| 实现 | 解析器函数 |
| 描述 | 基于正则表达式的解析器函数 |
| 作者 | Ryan Schmidt (Skizzerz留言) |
| 最新版本 | 2.0.0 (2020-03-18) |
| MediaWiki | 1.31+ |
| 许可证 | Public domain |
| 下載 | 用法 |
| 前往translatewiki.net翻譯RegexFunctions扩展 | |
RegexFunctions扩展提供基于正则表达式的解析器函数。
用法
截至目前,该扩展定义了三个解析器函数:rmatch、rsplit和rreplace。
#rmatch:{{#rmatch:string|pattern|then|else}}- string (必需) - 函数用于匹配的文本。
- pattern (必需) - 用于文本的正则表达式。
- then (可选) - 匹配成功时应返回什么。 $# 和 \# 包含捕获的文本。 若需一个组后跟另一个数字,请使用${#}#。
- else (可选) - 如果匹配未成功,应返回什么 默认为空字符串。
#rsplit:{{#rsplit:string|pattern|piece}}- string (必需) - 函数将切割的文本。
- pattern (必需) - 用于文本的正则表达式,不捕获任何文本。
- piece (可选) - 返回的片段,默认为0(第一个片段)。 负数表示从末尾开始返回片段。 例如,-1代表最后一块。
#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).
Examples
{{#rmatch:foo/bar|^foo/|starts with foo/|does not start with foo/}}→starts with foo/{{#rsplit:foo/bar/baz|/|-1}}→baz{{#rreplace:foo/bar|^(foo)/(bar)$|$2/$1}}→bar/foo
安裝
- 下载文件,并解压
RegexFunctions文件夹到extensions/目录中。
开发者和代码贡献人员应改从Git安装此扩展,输入:cd extensions/ git clone https://gerrit.wikimedia.org/r/mediawiki/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.
- Regex Fun - just another regex extension with more functionality but without any customization variables for limitations.
- MassEditRegex
| 此扩展在以下wiki农场/托管网站和/或软件包中提供: |
- Stable extensions/zh
- Parser function extensions/zh
- ParserFirstCallInit extensions/zh
- Public domain licensed extensions/zh
- Extensions in Wikimedia version control/zh
- All extensions/zh
- Extensions included in Canasta/zh
- Extensions included in MyWikis/zh
- Extensions included in ProWiki/zh
- Extensions included in wiki.gg/zh
