模板:Str match
跳至導覽
跳至搜尋
File:Test Template Info-Icon - Version (2).svg 模板文件[檢視] [編輯] [歷史] [清除快取]
| File:Lua-Logo.svg | 此模板使用Lua語言: |
簡介[編輯]
這個模板會將字串進行正則表達式匹配,並對正則表達式的匹配結果進行擷取組的輸出。如果正則表達式中沒有定義擷取組,則會輸出整個被正則表達式匹配到的字串。
若正則表達式沒有匹配到任何結果,則輸出結果為空字串。
- 這個模板使用的正則表達式是Lua的正則表達式。
用法[編輯]
{{str match|str= <!-- 欲進行匹配的字串 -->|ptn= <!-- 正則表達式,可以輸入以<nowiki>標籤包裹的維基代碼 -->
|match= 欲輸出擷取組的編號,預設為1|nomatch=沒有匹配到結果時輸出的預設內容,預設為空字串}}
範例[編輯]
{{str match|str=hello|ptn=%a(%a)%a}}
輸出:e
模板數據[編輯]
<templatedata> { "params": { "str": { "label": "待查字串", "description": "要查詢/匹配正則表達式的字串", "type": "string" }, "ptn": { "label": "正則表達式", "type": "string" }, "match": { "label": "捕獲組編號" }, "nomatch": { "label": "不匹配時", "description": "不匹配時輸出的文字,默認為空", "type": "string" } }, "description": "這個模板會將字串進行正則表達式匹配,並對正則表達式的匹配結果進行捕獲組的輸出。", "format": "inline" } </templatedata>