qr/STRING/msixpodualn-
This operator quotes (and possibly compiles) its STRING as a regular expression. STRING is interpolated the same way as PATTERN in
m/PATTERN/. If"'"is used as the delimiter, no variable interpolation is done. Returns a Perl value which may be used instead of the corresponding/STRING/msixpodualnexpression. The returned value is a normalized version of the original pattern. It magically differs from a string containing the same characters:ref(qr/x/)returns "Regexp"; however, dereferencing it is not well defined (you currently get the normalized version of the original pattern, but this may change).この演算子は STRING を正規表現としてクォートします (そして可能ならコンパイルします)。 STRING は
m/PATTERN/内の PATTERN と同様に文字変換されます。"'"がデリミタとして使用された場合、変数展開は行われません。 対応する/STRING/msixpodualn表現の代わりに使われた Perl の値を返します。 返り値は元のパターンを正規化したものです。 これは不思議なことに、同じ文字を含む文字列とは異なります:ref(qr/x/)は "Regexp" を返します; しかし、これのデリファレンスは 定義されていません (現在の所元のパターンの正規化版を返しますが、これは 変更されるかもしれません)。For example,
例えば:
$rex = qr/my.STRING/is; print $rex; # prints (?si-xm:my.STRING) s/$rex/foo/;is equivalent to
は以下と等価です:
s/my.STRING/foo/is;The result may be used as a subpattern in a match:
結果はマッチのサブパターンとして使えます:
$re = qr/$pattern/; $string =~ /foo${re}bar/; # can be interpolated in other # patterns $string =~ $re; # or used standalone $string =~ /$re/; # or this way$re = qr/$pattern/; $string =~ /foo${re}bar/; # 他のパターンに展開できる $string =~ $re; # または単独で使う $string =~ /$re/; # またはこのようにするSince Perl may compile the pattern at the moment of execution of the
qr()operator, usingqr()may have speed advantages in some situations, notably if the result ofqr()is used standalone:Perl は
qr()演算子を実行する瞬間にパターンをコンパイルするので、qr()を使うことでいくつかの場面で速度的に有利になります; 特にqr()の結果が独立して使われる場合に有利になります。sub match { my $patterns = shift; my @compiled = map qr/$_/i, @$patterns; grep { my $success = 0; foreach my $pat (@compiled) { $success = 1, last if /$pat/; } $success; } @_; }Precompilation of the pattern into an internal representation at the moment of
qr()avoids the need to recompile the pattern every time a match/$pat/is attempted. (Perl has many other internal optimizations, but none would be triggered in the above example if we did not useqr()operator.)qr()の時点でパターンを内部表現にプリコンパイルすることにより、/$pat/を試みる毎に毎回パターンを再コンパイルするのを 避けることができます。 (Perl はその他にも多くの内部最適化を行いますが、上の例でqr()演算子を 使わなかった場合はどの最適化も行われません。)Options (specified by the following modifiers) are:
(以下の修飾子で指定される)オプションは以下の通りです:
m Treat string as multiple lines. s Treat string as single line. (Make . match a newline) i Do case-insensitive pattern matching. x Use extended regular expressions; specifying two x's means \t and the SPACE character are ignored within square-bracketed character classes p When matching preserve a copy of the matched string so that ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH} will be defined (ignored starting in v5.20 as these are always defined starting in that release) o Compile pattern only once. a ASCII-restrict: Use ASCII for \d, \s, \w and [[:posix:]] character classes; specifying two a's adds the further restriction that no ASCII character will match a non-ASCII one under /i. l Use the current run-time locale's rules. u Use Unicode rules. d Use Unicode or native charset, as in 5.12 and earlier. n Non-capture mode. Don't let () fill in $1, $2, etc...m 文字列を複数行として扱う。 s 文字列を一行として扱う。 (. が 改行にマッチングするようにする) i パターンマッチにおいて大文字小文字を区別しない。 x 拡張正規表現を使う; x を二つ指定すると、 \t と SPACE 文字は大かっこ文字クラスの中では無視されます p マッチング時にマッチングした文字列を保存するので、 ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH} が定義される (このリリースから常に定義されるので、v5.20 から無視される) o 一度だけコンパイルする。 a ASCII 制限: \d, \s, \w, [[:posix:]] 文字クラスに ASCII を使う; a を二つ指定すると、/i で ASCII 文字が非 ASCII 文字に マッチングしないようなさらなる制限を追加する。 l 現在の実行時ロケールの規則を使う。 u Unicode の規則を使う。 d 5.12 以降かどうかで、Unicode かネイティブな文字集合を使う。 n 非捕捉モード。() で $1, $2 などを埋めないIf a precompiled pattern is embedded in a larger pattern then the effect of
"msixpluadn"will be propagated appropriately. The effect that the/omodifier has is not propagated, being restricted to those patterns explicitly using it.プリコンパイルされたパターンがより大きいパターンに組み込まれている場合、
"msixpluadn"の効果は適切に伝播します。/o修飾子の効果は伝播せず、明示的に使われたパターンに制限されます。The
/a,/d,/l, and/umodifiers (added in Perl 5.14) control the character set rules, but/ais the only one you are likely to want to specify explicitly; the other three are selected automatically by various pragmas.(Perl 5.14 で追加された)
/a,/d,/l,/u修飾子は 、 文字集合の規則を制御しますが、明示的に指定したいと思いそうなものは/aだけでしょう; その他の三つはさまざまなプラグマによって自動的に選択されます。See perlre for additional information on valid syntax for STRING, and for a detailed look at the semantics of regular expressions. In particular, all modifiers except the largely obsolete
/oare further explained in "Modifiers" in perlre./ois described in the next section.STRING として有効な文法に関する追加の情報と、正規表現の意味論に関する 詳細については、perlre を参照してください。 特に、かなり古いものである
/o以外の全ての修飾子については "Modifiers" in perlre でさらに説明されています。/oは次の節に記述されています。