本文へ移動
mie リファレンス
REPL Playground

std:regex リファレンス

@import 'std:regex' で取得する正規表現モジュール。パターンのコンパイル (compile) と、マッチ・抽出・置換・分割を持つ Regex object を提供する (RE2 構文・線形時間)。

compile

compile(pattern)
compile: { String | Result(Regex) }

パターン文字列をコンパイルし Result (成功時 Ok(Regex object)) を返す。

>>> regex.compile("a+").unwrap!.test("caat")
true

escape

escape(s)
escape: { String | String }

正規表現メタ文字をすべてエスケープしたリテラル一致のパターン断片を返す。

>>> regex.escape("1+1=2")
"1\\+1=2"

Regex メソッド

captures

re.captures(text)
captures: { String | Option(List(Option(String))) }

最左一致のグループ列 (添字 = グループ番号、0 は全体一致) を Option で返す。

>>> regex.compile("(a+)(b)?").unwrap!.captures("caat")
Some([Some("aa"), Some("aa"), None])

find

re.find(text)
find: { String | Option(String) }

最左の一致文字列を Option で返す (一致なしは None)。

>>> regex.compile("[0-9]+").unwrap!.find("abc123def")
Some("123")

find_all

re.find_all(text)
find_all: { String | List(String) }

重ならない全一致文字列を List で返す (一致なしは [])。

>>> regex.compile("[0-9]+").unwrap!.find_all("a1b22c333")
["1", "22", "333"]

replace

re.replace(text, repl)
replace: { String, String | String }

全一致をテンプレート repl ($1 等でグループ参照) で置換した String を返す。

>>> regex.compile("[0-9]+").unwrap!.replace("a12b34", "#")
"a#b#"

split

re.split(text)
split: { String | List(String) }

一致を区切りとして text を分割した List を返す (空要素も保持)。

>>> regex.compile("[0-9]+").unwrap!.split("a12b34")
["a", "b", ""]

test

re.test(text)
test: { String | Bool }

text 中に一致があるか Bool を返す。

>>> regex.compile("[0-9]+").unwrap!.test("abc123")
true