タイトル

Synopsis 5: Rules


著者

Damian Conway <damian [at] conway.org> and Allison Randal <al [at] shadowed.net>


バージョン

   Maintainer: Patrick Michaud <pmichaud [at] pobox.com>
   Date: 24 Jun 2002
   Last Modified: 25 Feb 2006
   Number: 5
   Version: 12

This document summarizes Apocalypse 5, which is about the new regex syntax. We now try to call them "rules" because they haven't been regular expressions for a long time. (The term "regex" is still acceptable.)

このドキュメントは、新たな正規表現構文(regex syntax)について述べている 黙示録(Apocalypse) 5の 要約です。私たちはそれを"ルール"と呼ぼうと考えています。なぜなら、 ながきに渡りそれはもはや正規表現ではないからです("regex"という言葉は 今でも受け入れられます)。

訳者注: ドキュメントはさらに新しくなっています (2006/4/13時点で Version 15)。 大きな変更はないようですが、 正確を期するなら オリジナル も参照してください。


新たな状態変数と捕獲変数

The underlying match state object is now available as the $/ variable, which is implicitly lexically scoped. All access to the current (or most recent) match is through this variable, even when it doesn't look like it. The individual capture variables (such as $0, $1, etc.) are just elements of $/.

基礎をなすマッチ状態オブジェクトは、 <$/>という変数として 扱うことが可能となりました。これは暗黙裡にレキシカルスコープを持ちます。 カレントの(あるいは至近の)マッチに対するすべてのアクセスは、 たとえそうは見えなかったときでも、この変数を通してなされます。 $0, $1などの独立した捕獲変数(caputure vairables)は、 マッチした文字列全体である$/の単なる要素です。

By the way, the numbered capture variables now start at $0, $1, $2, etc. See below.

ところで、数字付けされた捕獲変数は$0, $1, $2…のようになりました。 詳しくは後述します。


変更されていない構文的機構

The following regex features use the same syntax as in Perl 5:

以下の正規表現機能はPerl 5における構文と同じものを使います:


修飾子


変更されたメタキャラクタ


新しいメタキャラクタ


ブラケットの合理化


変数の展開/非展開


拡張可能なメタ構文 (<...>)


バックスラッシュの再構成


正規表現はルール


バックトラックの制御


名前によるキャプチャ


何もないのは不正


マッチからの戻り値

マッチオブジェクト

サブパターンのキャプチャ

キャプチャされたサブパターンへのアクセス

ネストしたサブパターンキャプチャ

数量化(quantify)されたサブパターンキャプチャ

間接的に数量化されたサブパターンキャプチャ

サブパターンの番号付け

サブルールのキャプチャ

キャプチャされたサブルールにアクセスする

同一のサブルールによる繰り返されたキャプチャ

別名付け(エイリアシング)

Aliases can be named or numbered. They can be scalar-, array-, or hash-like. And they can be applied to either capturing or non-capturing constructs. The following sections highlight special features of the semantics of some of those combinations.

別名は名前によっても番号によってもできます。 別名はスカラーでも、配列でも、はたまたハッシュのようにもできます。 そして別名は捕獲を行う構造に対してもできますし、 捕獲を行わない構造に対してもできます。 後続のセクションではこれらの組み合わせのいくつかの特別な機能を紹介します。

サブパターンを名前つきスカラーに別名付けする

名前付けされたエイリアスの非キャプチャブラケットに対する適用

サブルールに対する名前つきスカラーによる別名

番号付けされたスカラーによる別名付け

数量化された構造に対するスカラーによる別名付けの適用

配列エイリアシング

ハッシュエイリアシング

外部エイリアシング

繰り返されたマッチからのキャプチャ

:keepall


文法


文法的なカテゴリ

For writing your own backslash and assertion rules or macros, you may use the following syntactic categories:

自分用のバックスラッシュルールや表明ルール、マクロを記述するために、 以下のような構文カテゴリを使用することができます:

     rule rxbackslash:<w> { ... }    # define your own \w and \W
                                     # オリジナルの \w と \W を定義
     rule rxassertion:<*> { ... }    # define your own <*stuff>
                                     # オリジナルの C<*stuff> を定義
     macro rxmetachar:<,> { ... }    # define a new metacharacter
                                     # 新たなメタキャラクタを定義
     macro rxmodinternal:<x> { ... } # define your own /:x() stuff/
                                     # オリジナルの /:x() stuff/ を定義
     macro rxmodexternal:<x> { ... } # define your own m:x()/stuff/
                                     # オリジナルの m:x()/stuff/ を定義

As with any such syntactic shenanigans, the declaration must be visible in the lexical scope to have any effect. It's possible the internal/external distinction is just a trait, and that some of those things are subs or methods rather than rules or macros. (The numeric rxmods are recognized by fallback macros defined with an empty operator name.)

任意のこのような構文的なshenanigans(ペテン?)を使うことで、 定義はそのレキシカルスコープで可視になり定められた効果をもちます。 internal/externalの区別は単なる特性で、 それらはルールやマクロではなくサブルーチンやメソッドです (数値的な rxmods は 空の演算子名を使って定義された フォールバックマクロによって認識されます)。


プラグマ

The rx pragma may be used to control various aspects of regex compilation and usage not otherwise provided for.

rxプラグマは正規表現のコンパイルにおけるさまざまな様相や 提供されていない使い方を制御するために使うことができます。


音訳(Transliteration)


文字列でないものに対するマッチング