=encoding euc-jp =head1 NAME =begin original perlrebackslash - Perl Regular Expression Backslash Sequences and Escapes =end original perlrebackslash - Perl 正規表現逆スラッシュシーケンスとエスケープ =head1 DESCRIPTION =begin original The top level documentation about Perl regular expressions is found in L. =end original Perl 正規表現に関する最上位文書は L です。 =begin original This document describes all backslash and escape sequences. After explaining the role of the backslash, it lists all the sequences that have a special meaning in Perl regular expressions (in alphabetical order), then describes each of them. =end original この文書は全ての逆スラッシュシーケンスとエスケープシーケンスについて 記述します。 逆スラッシュの役割について説明したあと、Perl 正規表現の中で特別な意味を持つ 全てのシーケンスを(アルファベット順に)示し、それぞれについて記述します。 =begin original Most sequences are described in detail in different documents; the primary purpose of this document is to have a quick reference guide describing all backslash and escape sequences. =end original ほとんどのシーケンスは他の文書に詳細に記述されています; この文書の主な目的は 全ての逆スラッシュシーケンスとエスケープシーケンスを記述した クイックリファレンスガイドとなることです。 =head2 The backslash (逆スラッシュ) =begin original In a regular expression, the backslash can perform one of two tasks: it either takes away the special meaning of the character following it (for instance, C<\|> matches a vertical bar, it's not an alternation), or it is the start of a backslash or escape sequence. =end original 正規表現において、逆スラッシュは二つのタスクのどちらかを行います: 引き続く文字の特殊な意味を取り除く(例えば、(C<\|> は代替ではなく、垂直バーに マッチングします)か、逆スラッシュシーケンスまたはエスケープシーケンスの 開始となります。 =begin original The rules determining what it is are quite simple: if the character following the backslash is an ASCII punctuation (non-word) character (that is, anything that is not a letter, digit or underscore), then the backslash just takes away the special meaning (if any) of the character following it. =end original これが何かを決定する規則はかなり単純です: 逆スラッシュに引き続く文字が ASCII 句読点(非単語)文字(これは英字、数字、下線以外の全ての文字)なら、 逆スラッシュは引き続く文字の特別な意味を(もしあるなら)取り除きます。 =begin original If the character following the backslash is an ASCII letter or an ASCII digit, then the sequence may be special; if so, it's listed below. A few letters have not been used yet, so escaping them with a backslash doesn't change them to be special. A future version of Perl may assign a special meaning to them, so if you have warnings turned on, Perl will issue a warning if you use such a sequence. [1]. =end original 逆スラッシュに引き続く文字が ASCII 英字または ASCII 数字なら、この並びは 特別です; その場合、以下に示します。 いくつかの文字はまだ使われておらず、これを逆スラッシュでエスケープするのは 特別なものにはしません。 将来のバージョンの Perl はこれに特別な意味を割り当てるかもしれないので、 しかし、警告が有効なら、そのような並びを使うと Perl は警告を出力します。 [1]。 =begin original It is however guaranteed that backslash or escape sequences never have a punctuation character following the backslash, not now, and not in a future version of Perl 5. So it is safe to put a backslash in front of a non-word character. =end original しかし、逆スラッシュシーケンスやエスケープシーケンスが逆スラッシュの後に 句読点文字がくることがないことは今だけではなく将来のバージョンの Perl 5 でも保証されています。 従って、非単語文字の前に逆スラッシュを置くのは安全です。 =begin original Note that the backslash itself is special; if you want to match a backslash, you have to escape the backslash with a backslash: C matches a single backslash. =end original 逆スラッシュ自体が特別であることに注意してください; 逆スラッシュと マッチングしたいなら、逆スラッシュを逆スラッシュでエスケープする必要が あります: C は単一の逆スラッシュにマッチングします。 =over 4 =item [1] =begin original There is one exception. If you use an alphanumerical character as the delimiter of your pattern (which you probably shouldn't do for readability reasons), you will have to escape the delimiter if you want to match it. Perl won't warn then. See also L. =end original 一つの例外があります。 パターンの区切りとして英数字を使っている(おそらくこれは可読性の理由で するべきではありません)場合、その文字にマッチングしたいなら区切り文字を エスケープする必要があります。 Perl は警告を出しません。 L も参照してください。 =back =head2 All the sequences and escapes (全てのシーケンスとエスケープ) =begin original Those not usable within a bracketed character class (like C<[\da-z]>) are marked as C =end original (C<[\da-z]> のような) 大かっこ文字クラスで使えないものは C と 記しています。 =begin original \000 Octal escape sequence. \1 Absolute backreference. Not in []. \a Alarm or bell. \A Beginning of string. Not in []. \b Word/non-word boundary. (Backspace in []). \B Not a word/non-word boundary. Not in []. \cX Control-X (X can be any ASCII character). \C Single octet, even under UTF-8. Not in []. \d Character class for digits. \D Character class for non-digits. \e Escape character. \E Turn off \Q, \L and \U processing. Not in []. \f Form feed. \g{}, \g1 Named, absolute or relative backreference. Not in []. \G Pos assertion. Not in []. \h Character class for horizontal whitespace. \H Character class for non horizontal whitespace. \k{}, \k<>, \k'' Named backreference. Not in []. \K Keep the stuff left of \K. Not in []. \l Lowercase next character. Not in []. \L Lowercase till \E. Not in []. \n (Logical) newline character. \N Any character but newline. Experimental. Not in []. \N{} Named or numbered (Unicode) character. \p{}, \pP Character with the given Unicode property. \P{}, \PP Character without the given Unicode property. \Q Quotemeta till \E. Not in []. \r Return character. \R Generic new line. Not in []. \s Character class for whitespace. \S Character class for non whitespace. \t Tab character. \u Titlecase next character. Not in []. \U Uppercase till \E. Not in []. \v Character class for vertical whitespace. \V Character class for non vertical whitespace. \w Character class for word characters. \W Character class for non-word characters. \x{}, \x00 Hexadecimal escape sequence. \X Unicode "extended grapheme cluster". Not in []. \z End of string. Not in []. \Z End of string. Not in []. =end original \000 8 進数エスケープシーケンス。 \1 絶対後方参照。[] 内では使えない。 \a アラームまたはベル。 \A 文字列の先頭。[] 内では使えない。 \b 単語/非単語境界。([] 内では後退)。 \B 単語/非単語境界でない。[] 内では使えない。 \cX Control-X (X は任意の ASCII 文字)。 \C UTF-8 の下でも、単一のオクテット。[] 内では使えない。 \d 数字のための文字クラス。 \D 非数字のための文字クラス。 \e エスケープ文字。 \E \Q, \L, \U の処理を終了する。[] 内では使えない。 \f 書式送り。 \g{}, \g1 名前付きの絶対または相対後方参照。[] 内では使えない。 \G 位置表明。[] 内では使えない。 \h 水平空白のための文字クラス。 \H 非水平空白のための文字クラス。 \k{}, \k<>, \k'' 名前付き後方参照。[] 内では使えない。 \K \K の左側を保持する。[] 内では使えない。 \l 次の文字を小文字に。[] 内では使えない。 \L \E まで小文字に。[] 内では使えない。 \n (論理) 改行文字。 \N 改行以外の任意の文字。実験的。[] 内では使えない。 \N{} 名前付きまたは番号 (Unicode) 文字。 \p{}, \pP Unicode 特性付き文字。 \P{}, \PP Unicode 特性なし文字。 \Q \E までクォート。[] 内では使えない。 \r 復帰文字。 \R 一般的な改行。[] 内では使えない。 \s 空白のための文字クラス。 \S 非空白のための文字クラス。 \t タブ文字。 \u 辻の文字をタイトル文字に。[] 内では使えない。 \U \E まで大文字に。[] 内では使えない。 \v 垂直のための文字クラス。 \V 非垂直空白のための文字クラス。 \w 単語文字の文字クラス。 \W 非単語文字のための文字クラス。 \x{}, \x00 16 進数エスケープシーケンス。 \X 「拡張書記素クラスタ」。[] 内では使えない。 \z 文字列の末尾。[] 内では使えない。 \Z 文字列の末尾。[] 内では使えない。 =head2 Character Escapes (文字エスケープ) =head3 Fixed characters (固定文字) =begin original A handful of characters have a dedicated I. The following table shows them, along with their ASCII code points (in decimal and hex), their ASCII name, the control escape (see below) and a short description. =end original 一部の文字列は I<文字エスケープ> として動作します。 これらは、ASCII 符号位置 (10 進と 16 進)、ASCII の名前、制御エスケープ (後述)、短い説明と共に、以下の表に示しています。 =begin original Seq. Code Point ASCII Cntr Description. Dec Hex \a 7 07 BEL \cG alarm or bell \b 8 08 BS \cH backspace [1] \e 27 1B ESC \c[ escape character \f 12 0C FF \cL form feed \n 10 0A LF \cJ line feed [2] \r 13 0D CR \cM carriage return \t 9 09 TAB \cI tab =end original Seq. 符号位置 ASCII Cntr 説明。 10 進 16 進 \a 7 07 BEL \cG アラームまたはベル \b 8 08 BS \cH 後退 [1] \e 27 1B ESC \c[ エスケープ文字 \f 12 0C FF \cL 書式送り \n 10 0A LF \cJ 改行 [2] \r 13 0D CR \cM 復帰 \t 9 09 TAB \cI タブ =over 4 =item [1] =begin original C<\b> is only the backspace character inside a character class. Outside a character class, C<\b> is a word/non-word boundary. =end original C<\b> は文字クラスの中でのみ復帰文字です。 文字クラスの外側では、C<\b> は単語非単語境界です。 =item [2] =begin original C<\n> matches a logical newline. Perl will convert between C<\n> and your OSses native newline character when reading from or writing to text files. =end original C<\n> は論理改行にマッチングします。 Perl はテキストファイルの読み書きをする時には C<\n> OS ネイティブな 改行文字との間を変換します。 =back =head4 Example (例) $str =~ /\t/; # Matches if $str contains a (horizontal) tab. =head3 Control characters (制御文字) =begin original C<\c> is used to denote a control character; the character following C<\c> is the name of the control character. For instance, C matches the character I (a carriage return, code point 13). The case of the character following C<\c> doesn't matter: C<\cM> and C<\cm> match the same character. =end original C<\c> は制御文字を示すために使われます; C<\c> に引き続く文字は制御文字の 名前です。 例えば、C は I (復帰、符号位置 13) にマッチングします。 C<\c> に引き続く文字の大文字小文字は無視されます: C<\cM> と C<\cm> は同じ 文字にマッチングします。 =begin original Mnemonic: Iontrol character. =end original 記憶法: 制御文字(Iontrol character)。 =head4 Example (例) $str =~ /\cK/; # Matches if $str contains a vertical tab (control-K). =head3 Named or numbered characters (名前付きまたは番号による文字) =begin original All Unicode characters have a Unicode name and numeric ordinal value. Use the C<\N{}> construct to specify a character by either of these values. =end original 全ての Unicode 文字は Unicode 名と序数を持ちます。 文字をそれらの値で指定するには C<\N{}> 構文を使います。 =begin original To specify by name, the name of the character goes between the curly braces. In this case, you have to C to load the Unicode names of the characters, otherwise Perl will complain. =end original 名前を指定するには、文字の名前は中かっこの間に書きます。 この場合、文字の名前を読み込むには C とする必要があり、 さもなければ Perl は警告します。 =begin original To specify by Unicode ordinal number, use the form C<\N{U+I}>, where I is a number in hexadecimal that gives the ordinal number that Unicode has assigned to the desired character. It is customary (but not required) to use leading zeros to pad the number to 4 digits. Thus C<\N{U+0041}> means C, and you will rarely see it written without the two leading zeros. C<\N{U+0041}> means C even on EBCDIC machines (where the ordinal value of C is not 0x41). =end original Unicode 序数を指定するには、C<\N{U+I}> 形式を使います; ここで I は Unicode が目的の文字に割り当てている序数を 16 進数で指定します。 数値を 4 桁にパッディングするために先頭にゼロを付ける慣習があります (しかし必須ではありません)。 従って C<\N{U+0041}> は C を意味し、先頭の二つの ゼロなしで書かれるのを見ることは稀でしょう。 C<\N{U+0041}> は (C の序数が 0x41 でない) EBCDIC マシンでも C を 意味します。 =begin original It is even possible to give your own names to characters, and even to short sequences of characters. For details, see L. =end original 文字や文字の短い並びに独自の名前をつけることすら可能です。 詳しくは L を参照してください。 =begin original (There is an expanded internal form that you may see in debug output: C<\N{U+I.I...}>. The C<...> means any number of these Is separated by dots. This represents the sequence formed by the characters. This is an internal form only, subject to change, and you should not try to use it yourself.) =end original (デバッグ出力で見るかも知れない拡張内部形式 C<\N{U+I.I...}> があります。 C<...> は任意の数の I をドットで区切ったものです。 これは文字によって形成される並びを表現します。 これは内部形式専用で、変更されるかも知れず、自分自身で 使おうとするべきではありません。) =begin original Mnemonic: Iamed character. =end original 記憶法: 名前付き文字(Iamed character)。 =begin original Note that a character that is expressed as a named or numbered character is considered as a character without special meaning by the regex engine, and will match "as is". =end original 名前付きまたは番号による文字で表現される文字は正規表現エンジンにとって 特別な意味を持たない文字として扱われ、「そのまま」でマッチングすることに 注意してください。 =head4 Example (例) use charnames ':full'; # Loads the Unicode names. $str =~ /\N{THAI CHARACTER SO SO}/; # Matches the Thai SO SO character use charnames 'Cyrillic'; # Loads Cyrillic names. $str =~ /\N{ZHE}\N{KA}/; # Match "ZHE" followed by "KA". =head3 Octal escapes (8 進数エスケープ) =begin original Octal escapes consist of a backslash followed by two or three octal digits matching the code point of the character you want to use. This allows for 512 characters (C<\00> up to C<\777>) that can be expressed this way (but anything above C<\377> is deprecated). Enough in pre-Unicode days, but most Unicode characters cannot be escaped this way. =end original 8 進数エスケープは逆スラッシュに引き続いて使い多い文字の符号位置に マッチングする 2 または 3 文字の 8 進数からなります。 この方法で表現できる 512 文字 (C<\00> から C<\777>) が使えます (しかし C<\377> 以上は廃止予定です)。 Unicode 時代以前なら十分でしたが、ほとんどの Unicode 文字はこの方法では エスケープできません。 =begin original Note that a character that is expressed as an octal escape is considered as a character without special meaning by the regex engine, and will match "as is". =end original 8 進エスケープで表現される文字は正規表現エンジンにとって特別な意味を持たない 文字として考えられ、「そのまま」でマッチングすることに注意してください。 =head4 Examples (assuming an ASCII platform) (例(ASCII プラットフォームを仮定)) $str = "Perl"; $str =~ /\120/; # Match, "\120" is "P". $str =~ /\120+/; # Match, "\120" is "P", it is repeated at least once. $str =~ /P\053/; # No match, "\053" is "+" and taken literally. =head4 Caveat (注意) =begin original Octal escapes potentially clash with backreferences. They both consist of a backslash followed by numbers. So Perl has to use heuristics to determine whether it is a backreference or an octal escape. Perl uses the following rules: =end original 8 進エスケープは潜在的に後方参照を破壊します。 これらはどちらも逆スラッシュに引き続く数値で構成されます。 それで Perl は後方参照か 8 進エスケープかを決定するのに発見的手法を 使います。 Perl は以下の規則を使います: =over 4 =item 1 =begin original If the backslash is followed by a single digit, it's a backreference. =end original 逆スラッシュに引き続くのが単一の数字なら、それは後方参照です。 =item 2 =begin original If the first digit following the backslash is a 0, it's an octal escape. =end original 逆スラッシュに引き続く文字が 0 なら、それは 8 進エスケープです。 =item 3 =begin original If the number following the backslash is N (decimal), and Perl already has seen N capture groups, Perl will consider this to be a backreference. Otherwise, it will consider it to be an octal escape. Note that if N > 999, Perl only takes the first three digits for the octal escape; the rest is matched as is. =end original 逆スラッシュに引き続く数値が N (10 進) で、Perl がすでに N 個の 捕捉グループを見ているなら、Perl はこれを後方参照として扱います。 さもなければ、これは 8 進エスケープとして扱います。 N > 999 なら、Perl は 8 進エスケープとして最初の 3 桁のみを使うことに 注意してください; 残りはそのままマッチングされます。 my $pat = "(" x 999; $pat .= "a"; $pat .= ")" x 999; /^($pat)\1000$/; # Matches 'aa'; there are 1000 capture groups. /^$pat\1000$/; # Matches 'a@0'; there are 999 capture groups # and \1000 is seen as \100 (a '@') and a '0'. =back =head3 Hexadecimal escapes (16 進数エスケープ) =begin original Hexadecimal escapes start with C<\x> and are then either followed by a two digit hexadecimal number, or a hexadecimal number of arbitrary length surrounded by curly braces. The hexadecimal number is the code point of the character you want to express. =end original 16 進エスケープは C<\x> で始まり、2 桁の 16 進数あるいは中かっこで囲まれた 任意の桁の 16 進数が引き続きます。 16 進数は表現したい文字の符号位置です。 =begin original Note that a character that is expressed as a hexadecimal escape is considered as a character without special meaning by the regex engine, and will match "as is". =end original 16 進エスケープで表現される文字は正規表現エンジンにとって特別な意味を持たない 文字として考えられ、「そのまま」でマッチングすることに注意してください。 =begin original Mnemonic: heIadecimal. =end original 記憶法: 16 進数(heIadecimal)。 =head4 Examples (assuming an ASCII platform) (例(ASCII プラットフォームを仮定)) $str = "Perl"; $str =~ /\x50/; # Match, "\x50" is "P". $str =~ /\x50+/; # Match, "\x50" is "P", it is repeated at least once. $str =~ /P\x2B/; # No match, "\x2B" is "+" and taken literally. /\x{2603}\x{2602}/ # Snowman with an umbrella. # The Unicode character 2603 is a snowman, # the Unicode character 2602 is an umbrella. /\x{263B}/ # Black smiling face. /\x{263b}/ # Same, the hex digits A - F are case insensitive. =head2 Modifiers (修飾子) =begin original A number of backslash sequences have to do with changing the character, or characters following them. C<\l> will lowercase the character following it, while C<\u> will uppercase (or, more accurately, titlecase) the character following it. (They perform similar functionality as the functions C and C). =end original 多くの逆スラッシュシーケンスは、引き続く文字を変更します。 C<\l> は引き続く文字を小文字に変えます; 一方C<\u> は引き続く文字を大文字(より 正確にはタイトル文字)にします。 (これは関数 C および C と似たような機能を持ちます)。 =begin original To uppercase or lowercase several characters, one might want to use C<\L> or C<\U>, which will lowercase/uppercase all characters following them, until either the end of the pattern, or the next occurrence of C<\E>, whatever comes first. They perform similar functionality as the functions C and C do. =end original 複数の文字を大文字または小文字にするのに、C<\L> または C<\U> を 使いたいかもしれません; これは、パターンの末尾または次に C<\E> が現れるまでの全ての文字を小文字/大文字にします。 これらは関数 C および C が行うのと同じような機能を持ちます。 =begin original C<\Q> is used to escape all characters following, up to the next C<\E> or the end of the pattern. C<\Q> adds a backslash to any character that isn't a letter, digit or underscore. This will ensure that any character between C<\Q> and C<\E> is matched literally, and will not be interpreted by the regexp engine. =end original C<\Q> は引き続く文字列を、次の C<\E> またはパターンの最後まで エスケープします。 C<\Q> は英字、数字、下線でない全ての文字に逆スラッシュを追加します。 これにより C<\Q> と C<\E> の間の全ての文字はリテラルにマッチングし、正規表現 エンジンによって解釈されません。 =begin original Mnemonic: Iowercase, Ippercase, Iuotemeta, Ind. =end original 記憶法: 小文字(Iowercase)、大文字(Ippercase)、クォート(Iuotemeta)、 終了(Ind)。 =head4 Examples (例) $sid = "sid"; $greg = "GrEg"; $miranda = "(Miranda)"; $str =~ /\u$sid/; # Matches 'Sid' $str =~ /\L$greg/; # Matches 'greg' $str =~ /\Q$miranda\E/; # Matches '(Miranda)', as if the pattern # had been written as /\(Miranda\)/ =head2 Character classes (文字クラス) =begin original Perl regular expressions have a large range of character classes. Some of the character classes are written as a backslash sequence. We will briefly discuss those here; full details of character classes can be found in L. =end original Perl の正規表現は広い範囲の文字クラスを持っています。 文字クラスの一部は逆スラッシュシーケンスとして書かれます。 ここではそれらについて簡潔に議論します; 文字クラスに関する完全な詳細は L にあります。 =begin original C<\w> is a character class that matches any single I character (letters, digits, underscore). C<\d> is a character class that matches any decimal digit, while the character class C<\s> matches any whitespace character. New in perl 5.10.0 are the classes C<\h> and C<\v> which match horizontal and vertical whitespace characters. =end original C<\w> は単一の任意の I<単語> 文字 (英字、数字、下線) にマッチングする 文字クラスです。 C<\d> は 10 進数字にマッチングする文字クラスです; 一方文字クラス C<\s> は 空白文字にマッチングします。 perl 5.10.0 から導入されたのは、それぞれ水平空白文字と垂直空白文字に マッチングする C<\h> と C<\v> です。 =begin original The uppercase variants (C<\W>, C<\D>, C<\S>, C<\H>, and C<\V>) are character classes that match any character that isn't a word character, digit, whitespace, horizontal whitespace nor vertical whitespace. =end original 大文字の変種 (C<\W>, C<\D>, C<\S>, C<\H>, C<\V>) はそれぞれ単語文字、数字、 空白、水平空白、垂直空白ではない任意の文字にマッチングします。 =begin original Mnemonics: Iord, Iigit, Ipace, Iorizontal, Iertical. =end original 記憶法: 単語(Iord)、数字(Iigit)、空白(Ipace)、水平(Iorizontal)、 垂直(Iertical)。 =head3 Unicode classes (Unicode クラス) =begin original C<\pP> (where C

is a single letter) and C<\p{Property}> are used to match a character that matches the given Unicode property; properties include things like "letter", or "thai character". Capitalizing the sequence to C<\PP> and C<\P{Property}> make the sequence match a character that doesn't match the given Unicode property. For more details, see L and L. =end original C<\pP> (ここで C

は単一の文字) と C<\p{Property}> は与えられた Unicode 特性にマッチングする文字にマッチングしていました; 特性には "letter" や "thai character" のようなものが含まれます。 並びを C<\PP> と C<\P{Property}> のようにキャピタライズすると、与えられた Unicode 特性にマッチングしない文字にマッチングするようになります。 さらなる詳細については、L と L を参照してください。 =begin original Mnemonic: I

roperty. =end original 記憶法: 特性(I

roperty)。 =head2 Referencing (参照) =begin original If capturing parenthesis are used in a regular expression, we can refer to the part of the source string that was matched, and match exactly the same thing. There are three ways of referring to such I: absolutely, relatively, and by name. =end original 正規表現で捕捉かっこが使われると、マッチングしたソース文字列の一部を 参照したり、全く同じものにマッチングしたりできます。 このような I<後方参照> を参照するには三つの方法があります: 絶対、相対、 名前付きです。 =for later add link to perlrecapture =head3 Absolute referencing (絶対参照) =begin original A backslash sequence that starts with a backslash and is followed by a number is an absolute reference (but be aware of the caveat mentioned above). If the number is I, it refers to the Nth set of parentheses - whatever has been matched by that set of parenthesis has to be matched by the C<\N> as well. =end original 逆スラッシュで始まり、数値が引き続く逆スラッシュシーケンスは絶対参照です (しかし上述した注意を参照してください)。 数値が I なら、これは N 番目のかっこの組を参照します - 同様にこのかっこの 組でマッチングしたものは C<\N> でマッチングしたものである必要があります。 =head4 Examples (例) /(\w+) \1/; # Finds a duplicated word, (e.g. "cat cat"). /(.)(.)\2\1/; # Match a four letter palindrome (e.g. "ABBA"). =head3 Relative referencing (相対参照) =begin original New in perl 5.10.0 is a different way of referring to capture buffers: C<\g>. C<\g> takes a number as argument, with the number in curly braces (the braces are optional). If the number (N) does not have a sign, it's a reference to the Nth capture group (so C<\g{2}> is equivalent to C<\2> - except that C<\g> always refers to a capture group and will never be seen as an octal escape). If the number is negative, the reference is relative, referring to the Nth group before the C<\g{-N}>. =end original perl 5.10.0 からの新機能は捕捉バッファを参照する異なった方法です: C<\g> です。 C<\g> は引数として数値を取ります; 数値は中かっこでかこまれます(中かっこは オプションです)。 数値 (N) に符号がなければ、これは N 番目の捕捉グループです (従って C<\g{2}> は C<\2> と等価です - 但し C<\g> は常に捕捉グループへの参照であり、 8 進エスケープになることはありません)。 数値が負数なら、参照は相対で、C<\g{-N}> の N 個前のグループを参照します。 =begin original The big advantage of C<\g{-N}> is that it makes it much easier to write patterns with references that can be interpolated in larger patterns, even if the larger pattern also contains capture groups. =end original C<\g{-N}> の大きな利点は、捕捉グループを含んでいるような大きなパターンに 展開されるような参照のパターンをより簡単に書けることです。 =begin original Mnemonic: Iroup. =end original 記憶法: グループ(Iroup)。 =head4 Examples (例) /(A) # Buffer 1 ( # Buffer 2 (B) # Buffer 3 \g{-1} # Refers to buffer 3 (B) \g{-3} # Refers to buffer 1 (A) ) /x; # Matches "ABBA". my $qr = qr /(.)(.)\g{-2}\g{-1}/; # Matches 'abab', 'cdcd', etc. /$qr$qr/ # Matches 'ababcdcd'. =head3 Named referencing (名前付き参照) =begin original Also new in perl 5.10.0 is the use of named capture buffers, which can be referred to by name. This is done with C<\g{name}>, which is a backreference to the capture buffer with the name I. =end original もう一つの perl 5.10.0 からの新機能は、名前で参照できる名前付き捕捉バッファの 使用です。 これは C<\g{name}> として行われます; これは I という名前の 捕捉バッファへの後方参照です。 =begin original To be compatible with .Net regular expressions, C<\g{name}> may also be written as C<\k{name}>, C<< \k >> or C<\k'name'>. =end original .Net 正規表現との互換性のために、C<\g{name}> は C<\k{name}>, C<< \k >>, C<\k'name'> とも書けます。 =begin original Note that C<\g{}> has the potential to be ambiguous, as it could be a named reference, or an absolute or relative reference (if its argument is numeric). However, names are not allowed to start with digits, nor are they allowed to contain a hyphen, so there is no ambiguity. =end original C<\g{}> は潜在的には曖昧であることに注意してください; 名前付き 参照かもしれませんし、(引数が数値なら)絶対または相対参照かもしれません。 しかし、名前は数字で始めたり、ハイフンを含めたりすることはできないので、 ここに曖昧さはありません。 =head4 Examples (例) /(?\w+) \g{word}/ # Finds duplicated word, (e.g. "cat cat") /(?\w+) \k{word}/ # Same. /(?\w+) \k/ # Same. /(?.)(?.)\g{letter2}\g{letter1}/ # Match a four letter palindrome (e.g. "ABBA") =head2 Assertions (表明) =begin original Assertions are conditions that have to be true; they don't actually match parts of the substring. There are six assertions that are written as backslash sequences. =end original 表明は真である必要がある条件です; 実際に部分文字列の一部に マッチングすることはありません。 逆スラッシュシーケンスとして書かれる六つの表明があります。 =over 4 =item \A =begin original C<\A> only matches at the beginning of the string. If the C modifier isn't used, then C is equivalent with C. However, if the C modifier is used, then C matches internal newlines, but the meaning of C isn't changed by the C modifier. C<\A> matches at the beginning of the string regardless whether the C modifier is used. =end original C<\A> は文字列の先頭にのみマッチングします。 C 修飾子が使われていなければ、C は C と等価です。 しかし、C 修飾子が使われると、C は内部改行にマッチングしますが、 C の意味は C 修飾子によって変わりません。 C<\A> は、C 修飾子が使われるかどうかに関わらず文字列の先頭に マッチングします。 =item \z, \Z =begin original C<\z> and C<\Z> match at the end of the string. If the C modifier isn't used, then C is equivalent with C, that is, it matches at the end of the string, or before the newline at the end of the string. If the C modifier is used, then C matches at internal newlines, but the meaning of C isn't changed by the C modifier. C<\Z> matches at the end of the string (or just before a trailing newline) regardless whether the C modifier is used. =end original C<\z> と C<\Z> は文字列の末尾にマッチングします。 C 修飾子が使われていなければ、C は C と等価で、文字列の 末尾か、文字列の末尾の改行の手前にマッチングします。 C 修飾子が使われると、C は内部改行にマッチングしますが、 C の意味は C 修飾子によって変わりません。 C<\Z> は、C 修飾子が使われるかどうかに関わらず文字列の末尾(あるいは 末尾の改行の直前)にマッチングします。 =begin original C<\z> is just like C<\Z>, except that it will not match before a trailing newline. C<\z> will only match at the end of the string - regardless of the modifiers used, and not before a newline. =end original C<\z> は C<\Z> と同様ですが、末尾の改行の手前ではマッチングしません。 C<\z> は文字列の末尾にのみマッチングします - 修飾子が使われるかや改行の 前かに関わりません。 =item \G =begin original C<\G> is usually only used in combination with the C modifier. If the C modifier is used (and the match is done in scalar context), Perl will remember where in the source string the last match ended, and the next time, it will start the match from where it ended the previous time. =end original C<\G> は普通は C 修飾子との組み合わせでのみ使われます。 C 修飾子が使われると(そしてマッチングがスカラ根的宇土で行われると)、 Perl は最後のマッチングが終了したソース文字列の位置を覚えていて、次回の マッチングでは、前回終わった位置からマッチングを開始します。 =begin original C<\G> matches the point where the previous match ended, or the beginning of the string if there was no previous match. =end original C<\G> は前回のマッチングが終了した位置、あるいは前回にマッチングがなければ 文字列の先頭にマッチングします。 =for later add link to perlremodifiers =begin original Mnemonic: Ilobal. =end original 記憶法: グローバル(Ilobal)。 =item \b, \B =begin original C<\b> matches at any place between a word and a non-word character; C<\B> matches at any place between characters where C<\b> doesn't match. C<\b> and C<\B> assume there's a non-word character before the beginning and after the end of the source string; so C<\b> will match at the beginning (or end) of the source string if the source string begins (or ends) with a word character. Otherwise, C<\B> will match. =end original C<\b> は単語文字と非単語文字の境界にマッチングします; C<\B> は C<\b> が マッチングしない任意の文字間にマッチングします。 C<\b> と C<\B> は、ソース文字列の先頭の前と末尾の後ろに非単語文字があると 仮定します; 従って C<\b> は、ソース文字列が単語文字で始まっている(または 終わっている)場合はソース文字列の先頭(または末尾)にマッチングします。 さもなければ、C<\B> がマッチングします。 =begin original Mnemonic: Ioundary. =end original 記憶法: 境界(Ioundary)。 =back =head4 Examples (例) "cat" =~ /\Acat/; # Match. "cat" =~ /cat\Z/; # Match. "cat\n" =~ /cat\Z/; # Match. "cat\n" =~ /cat\z/; # No match. "cat" =~ /\bcat\b/; # Matches. "cats" =~ /\bcat\b/; # No match. "cat" =~ /\bcat\B/; # No match. "cats" =~ /\bcat\B/; # Match. while ("cat dog" =~ /(\w+)/g) { print $1; # Prints 'catdog' } while ("cat dog" =~ /\G(\w+)/g) { print $1; # Prints 'cat' } =head2 Misc (その他) =begin original Here we document the backslash sequences that don't fall in one of the categories above. They are: =end original ここでは上述のカテゴリの一つに当てはまらない逆スラッシュシーケンスについて 記述します。 それは: =over 4 =item \C =begin original C<\C> always matches a single octet, even if the source string is encoded in UTF-8 format, and the character to be matched is a multi-octet character. C<\C> was introduced in perl 5.6. =end original C<\C> は、例えソース文字列が UTF-8 形式でエンコードされていても、 常に単一のオクテットにマッチングし、マッチングする文字は複数オクテット 文字です。 C<\C> は perl 5.6 で導入されました。 =begin original Mnemonic: oItet. =end original 記憶法: オクテット(oItet)。 =item \K =begin original This is new in perl 5.10.0. Anything that is matched left of C<\K> is not included in C<$&> - and will not be replaced if the pattern is used in a substitution. This will allow you to write C instead of C or C. =end original これは perl 5.10.0 からの新しいものです。 C<\K> の左側でマッチングしたものは C<$&> に含まれません - そして パターンが置換で使われた場合は置き換えられません。 これにより、C or C の 代わりに C と書けます。 =begin original Mnemonic: Ieep. =end original Mnemonic: 保持(Ieep)。 =item \N =begin original This is a new experimental feature in perl 5.12.0. It matches any character that is not a newline. It is a short-hand for writing C<[^\n]>, and is identical to the C<.> metasymbol, except under the C flag, which changes the meaning of C<.>, but not C<\N>. =end original これは perl 5.12.0 の新しい実験的な機能です。 これは改行でない任意の文字にマッチングします。 これは C<[^\n]> の省略形で、C<.> メタ文字と同じですが、C<.> の意味を変える C フラグが付いていても C<\N> の意味は変わりません。 =begin original Note that C<\N{...}> can mean a L. =end original C<\N{...}> は L<名前または番号の文字|/Named or numbered characters> を 意味するかも知れないことに注意してください。 =begin original Mnemonic: Complement of I<\n>. =end original 記憶法: I<\n> の補数。 =item \R X<\R> =begin original C<\R> matches a I, that is, anything that is considered a newline by Unicode. This includes all characters matched by C<\v> (vertical whitespace), and the multi character sequence C<"\x0D\x0A"> (carriage return followed by a line feed, aka the network newline, or the newline used in Windows text files). C<\R> is equivalent to C<< (?>\x0D\x0A)|\v) >>. Since C<\R> can match a sequence of more than one character, it cannot be put inside a bracketed character class; C is an error; use C<\v> instead. C<\R> was introduced in perl 5.10.0. =end original C<\R> は I<一般的な改行> にマッチングします; これは Unicode で改行として 扱われるものです。 これには C<\v> (垂直空白) でマッチングする全ての文字、複数文字並び C<"\x0D\x0A"> (復帰に引き続いて改行、またの名をネットワーク改行、 または Windows テキストファイルで使われる改行) を含みます。 C<\R> は C<< (?>\x0D\x0A)|\v) >> と等価です。 C<\R> は複数の文字の並びにマッチングするので、大かっこ文字クラスの内側には 使えません; C はエラーです; 代わりに C<\v> を使ってください。 C<\R> は perl 5.10.0 で導入されました。 =begin original Mnemonic: none really. C<\R> was picked because PCRE already uses C<\R>, and more importantly because Unicode recommends such a regular expression metacharacter, and suggests C<\R> as the notation. =end original 記憶法: 実際のところありません。 C<\R> が選ばれたのは、PCRE が既に C<\R> を使っていて、さらに重要なのは Unicode がこのような正規表現メタ文字を勧めていて、記法として C<\R> を 提案しているからです。 =item \X X<\X> =begin original This matches a Unicode I. =end original これは Unicode の I<拡張書記素クラスタ> (extended grapheme cluster) に マッチングします。 =begin original C<\X> matches quite well what normal (non-Unicode-programmer) usage would consider a single character. As an example, consider a G with some sort of diacritic mark, such as an arrow. There is no such single character in Unicode, but one can be composed by using a G followed by a Unicode "COMBINING UPWARDS ARROW BELOW", and would be displayed by Unicode-aware software as if it were a single character. =end original C<\X> は普通の (非 Unicode プログラマの) 使い方で単一の文字と考えられるものに かなりうまくマッチングします。 例えば、矢印のような、何らかのダイアクリティカルマーク付きの G を考えます。 Unicode にはそのような単一の文字はありませんが、G に引き続いて Unicode の "COMBINING UPWARDS ARROW BELOW" を使うことで合成でき、Unicode を認識する ソフトウェアに依って単一の文字であるかのように表示されます。 =begin original Mnemonic: eItended Unicode character. =end original 記憶法: 拡張 Unicode 文字(eItended Unicode character)。 =back =head4 Examples (例) "\x{256}" =~ /^\C\C$/; # Match as chr (256) takes 2 octets in UTF-8. $str =~ s/foo\Kbar/baz/g; # Change any 'bar' following a 'foo' to 'baz'. $str =~ s/(.)\K\1//g; # Delete duplicated characters. "\n" =~ /^\R$/; # Match, \n is a generic newline. "\r" =~ /^\R$/; # Match, \r is a generic newline. "\r\n" =~ /^\R$/; # Match, \r\n is a generic newline. "P\x{0307}" =~ /^\X$/ # \X matches a P with a dot above. =begin meta Translate: SHIRAKATA Kentaro (5.10.1-) Status: completed =end meta =cut