regex のフィールドとメソッド
フィールドは、値についての単純な事実で、ドットを使い角括弧なしで読み取ります: $value.name。メソッドは引数を取るか処理を行い、-> で呼び出します: $value->name(arguments)。または、値をパイプで渡して呼び出します: $value |> name(arguments)。
フィールド
| フィールド | 型 | 説明 |
|---|
.flags | string | フラグ(例: i)です。 |
.pattern | string | スラッシュやフラグを除いたパターンです。 |
KScript.Core から
#regex->match(text: string) → map
最初の一致: { text, index, groups, captures }。groups には名前付きグループ (?<name>...) が、captures には番号付きグループが格納されます。一致がない場合は null。
例
<echo>{ /(\d{4})-(\d\d)/->match('Due 2026-11').groups }</echo>
echo /(\d{4})-(\d\d)/->match('Due 2026-11').groups
#regex->match_all(text: string) → list
すべての一致を { text, index, groups, captures } のマップとして返します。
例
<echo>{ /\d+/->match_all('3 cats, 12 dogs')->map(fn(m) => $m.text) }</echo>
echo /\d+/->match_all('3 cats, 12 dogs')->map(fn(m) => $m.text)
#regex->replace(text: string, with) → string
The text with every match replaced. with is text, where $1 and $ are groups, or fn(match) returning text.
| パラメーター | 受け取るもの |
|---|
text 必須 | string |
with 必須 | 任意の値 |
引数には名前を付けられます: ->replace(…, with: …)。
例
<echo>{ /(\d{4})-(\d\d)/->replace('2026-11', '$2/$1') }</echo>
echo /(\d{4})-(\d\d)/->replace('2026-11', '$2/$1')
#regex->split(text: string) → list
文字列を一致箇所ごとに分割します。
例
<echo>{ /\s*[,;]\s*/->split('tea, coffee;milk') }</echo>
echo /\s*[,;]\s*/->split('tea, coffee;milk')
#regex->test(text: string) → bool
文字列がどこかに一致すれば真です(全体に一致させるには ^ と $ を使います)。
例
<echo>{ /^[a-z]+$/i->test('Hello') }</echo>
echo /^[a-z]+$/i->test('Hello')
any のメソッド
string のメソッド