模板:Str match/doc

出自Local Chinese Wikipedia
跳至導覽 跳至搜尋

簡介[編輯]

這個模板會將字串進行正則表達式匹配,並對正則表達式的匹配結果進行擷取組的輸出。如果正則表達式中沒有定義擷取組,則會輸出整個被正則表達式匹配到的字串。

若正則表達式沒有匹配到任何結果,則輸出結果為空字串

這個模板使用的正則表達式是Lua的正則表達式

用法[編輯]

{{str match|str= <!-- 欲進行匹配的字串 -->|ptn= <!-- 正則表達式,可以輸入以<nowiki>標籤包裹的維基代碼 -->
|match= 欲輸出擷取組的編號,預設為1|nomatch=沒有匹配到結果時輸出的預設內容,預設為空字串}}

範例[編輯]

{{str match|str=hello|ptn=%a(%a)%a}}

輸出:e

模板數據[編輯]

以下是該模板的模板資料,適用於視覺化編輯器等工具。

Str match模板資料

<templatedata> { "params": { "str": { "label": "待查字串", "description": "要查詢/匹配正則表達式的字串", "type": "string" }, "ptn": { "label": "正則表達式", "type": "string" }, "match": { "label": "捕獲組編號" }, "nomatch": { "label": "不匹配時", "description": "不匹配時輸出的文字,默認為空", "type": "string" } }, "description": "這個模板會將字串進行正則表達式匹配,並對正則表達式的匹配結果進行捕獲組的輸出。", "format": "inline" } </templatedata>

參見[編輯]

Template:輔助模板