=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 any special meaning 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 issues 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 alphanumeric character as the delimiter of your pattern (which you probably shouldn't do for readability reasons), you 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. See also \o{}. \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 \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. \F Foldcase till \E. Not in []. \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 or sequence. \o{} Octal escape sequence. \p{}, \pP Character with the given Unicode property. \P{}, \PP Character without the given Unicode property. \Q Quote (disable) pattern metacharacters 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 進数エスケープシーケンス。\o{} も参照。 \1 絶対後方参照。[] 内では使えない。 \a アラームまたはベル。 \A 文字列の先頭。[] 内では使えない。 \b 単語/非単語境界。([] 内では後退)。 \B 単語/非単語境界でない。[] 内では使えない。 \cX Control-X \C UTF-8 の下でも、単一のオクテット。[] 内では使えない。 \d 数字のための文字クラス。 \D 非数字のための文字クラス。 \e エスケープ文字。 \E \Q, \L, \U の処理を終了する。[] 内では使えない。 \f 書式送り。 \F \E まで畳み込み文字。[] 内では使えない。 \g{}, \g1 名前付きの絶対または相対後方参照。[] 内では使えない。 \G 位置表明。[] 内では使えない。 \h 水平空白のための文字クラス。 \H 非水平空白のための文字クラス。 \k{}, \k<>, \k'' 名前付き後方参照。[] 内では使えない。 \K \K の左側を保持する。[] 内では使えない。 \l 次の文字を小文字に。[] 内では使えない。 \L \E まで小文字に。[] 内では使えない。 \n (論理) 改行文字。 \N 改行以外の任意の文字。実験的。[] 内では使えない。 \N{} 名前付きまたは番号 (Unicode) 文字または並び。 \o{} 8 進数エスケープシーケンス。 \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 on ASCII platforms and a short description. (For EBCDIC platforms, see L.) =end original 一部の文字列は I<文字エスケープ> として動作します。 これらは、ASCII 符号位置 (10 進と 16 進)、ASCII の名前、ASCII プラットフォームでの制御エスケープ、短い説明と共に、以下の表に示しています。 (EBCDIC プラットフォームについては、L を 参照してください。) =begin original Seq. Code Point ASCII Cntrl 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 Cntrl 説明。 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 the backspace character only 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 converts between C<\n> and your OS's 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> determines the value of the construct. For example the value of C<\cA> is C, and the value of C<\cb> is C, etc. The gory details are in L. A complete list of what C, etc. means for ASCII and EBCDIC platforms is in L. =end original C<\c> は制御文字を示すために使われます; C<\c> に引き続く文字は 構文の値を決定します。 例えば C<\cA> の値は C、C<\cb> の値は C、などです。 内部の詳細は L にあります。 ASCII と EBCDIC プラットフォームで C などが意味するものの完全な 一覧は L にあります。 =begin original Note that C<\c\> alone at the end of a regular expression (or doubled-quoted string) is not valid. The backslash must be followed by another character. That is, C<\c\I> means C'> for all characters I. =end original 正規表現 (またはダブルクォートされた文字列) の末尾に単体で C<\c\> を置くのは 不正です。 逆スラッシュには他の文字が引き続かなければなりません。 つまり、全ての文字 I に対して C<\c\I> は C' を意味する> ということです。 =begin original To write platform-independent code, you must use C<\N{I}> instead, like C<\N{ESCAPE}> or C<\N{U+001B}>, see L. =end original プラットフォーム独立なコードを書くためには、代わりに C<\N{ESCAPE}> や C<\N{U+001B}> のような C<\N{I}> を使わなければなりません; L を参照してください。 =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 and character sequences (名前付きまたは番号による文字および文字並び) =begin original Unicode characters have a Unicode name and numeric code point (ordinal) value. Use the C<\N{}> construct to specify a character by either of these values. Certain sequences of characters also have names. =end original Unicode 文字は Unicode 名と数値の符号位置(序数)を持ちます。 文字をそれらの値で指定するには C<\N{}> 構文を使います。 一部の文字並びにも名前があります。 =begin original To specify by name, the name of the character or character sequence goes between the curly braces. =end original 名前を指定するには、文字や文字並びの名前は中かっこの間に書きます。 =begin original To specify a character by Unicode code point, use the form C<\N{U+I}>, where I is a number in hexadecimal that gives the code point 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 "A" even on EBCDIC machines (where the ordinal value of "A" is not 0x41). =end original 文字を Unicode 符号位置で指定するには、C<\N{U+I}> 形式を使います; ここで I は Unicode が目的の文字に割り当てている符号位置を 16 進数で指定します。 数値を 4 桁にパッディングするために先頭にゼロを付ける慣習がありますが、 必須ではありません。 従って C<\N{U+0041}> は C を意味し、先頭の二つの ゼロなしで書かれるのを見ることは稀でしょう。 C<\N{U+0041}> は ("A" の序数が 0x41 でない) EBCDIC マシンでも "A" を 意味します。 =begin original It is even possible to give your own names to characters and character sequences. 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 or character sequence expressed as a named or numbered character is considered a character without special meaning by the regex engine, and will match "as is". =end original 名前付きまたは番号による文字または文字並びで表現される文字は 正規表現エンジンにとって特別な意味を持たない文字として扱われ、 「そのまま」でマッチングすることに注意してください。 =head4 Example (例) $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 There are two forms of octal escapes. Each is used to specify a character by its code point specified in octal notation. =end original 8 進数エスケープには二つの形式があります。 それぞれは 8 進記法で指定された符号位置で文字を指定するために使われます。 =begin original One form, available starting in Perl 5.14 looks like C<\o{...}>, where the dots represent one or more octal digits. It can be used for any Unicode character. =end original Perl 5.14 から利用可能になった一つ目の形式は C<\o{...}> のようなもので、 ドットは一つ以上の 8 進数を表現します。 これはどのような Unicode 文字にも使えます。 =begin original It was introduced to avoid the potential problems with the other form, available in all Perls. That form consists of a backslash followed by three octal digits. One problem with this form is that it can look exactly like an old-style backreference (see L below.) You can avoid this by making the first of the three digits always a zero, but that makes \077 the largest code point specifiable. =end original これは全ての Perl で利用可能なもう一つの形式の潜在的な問題を避けるために 導入されました。 この形式は逆スラッシュに引き続いて 3 桁の 8 進数です。 この形式の問題の一つは、古い形式の後方参照とまったく同じように見えることです (以下の L を参照してください。) 3 桁の数字の最初の文字を常にゼロにすることにこれを避けられますが、指定できる 最大の符号位置が \077 になります。 =begin original In some contexts, a backslash followed by two or even one octal digits may be interpreted as an octal escape, sometimes with a warning, and because of some bugs, sometimes with surprising results. Also, if you are creating a regex out of smaller snippets concatenated together, and you use fewer than three digits, the beginning of one snippet may be interpreted as adding digits to the ending of the snippet before it. See L for more discussion and examples of the snippet problem. =end original ある種のコンテキストでは、逆スラッシュに引き続いて二つや一つの 8 進数が 8 進エスケープとして解釈され、時々警告が出て、ある種のバグにより、時々 驚くような結果になります。 また、より小さいスニペットを結合して正規表現を作成して、3 桁より小さいものを 使うと、一つのスニペットの最初がその前のスニペットの終わりに追加された 数値と解釈されることがあります。 スニペット問題に関するさらなる議論と例については L を 参照してください。 =begin original Note that a character expressed as an octal escape is considered a character without special meaning by the regex engine, and will match "as is". =end original 8 進エスケープで表現される文字は正規表現エンジンにとって特別な意味を持たない 文字として考えられ、「そのまま」でマッチングすることに注意してください。 =begin original To summarize, the C<\o{}> form is always safe to use, and the other form is safe to use for code points through \077 when you use exactly three digits to specify them. =end original まとめると、C<\o{}> 形式は常に安全で、もう一つの形式は、指定するときに 正確に 3 桁を使う場合 \077 までの符号位置なら安全です。 =begin original Mnemonic: I<0>ctal or Ictal. =end original 記憶法: 8 進数(I<0>ctal または Ictal)。 =head4 Examples (assuming an ASCII platform) (例(ASCII プラットフォームを仮定)) $str = "Perl"; $str =~ /\o{120}/; # Match, "\120" is "P". $str =~ /\120/; # Same. $str =~ /\o{120}+/; # Match, "\120" is "P", it's repeated at least once $str =~ /\120+/; # Same. $str =~ /P\053/; # No match, "\053" is "+" and taken literally. /\o{23073}/ # Black foreground, white background smiling face. /\o{4801234567}/ # Raises a warning, and yields chr(4) =head4 Disambiguation rules between old-style octal escapes and backreferences (古い形式の 8 進エスケープと後方参照との曖昧さをなくす規則) =begin original Octal escapes of the C<\000> form outside of bracketed character classes potentially clash with old-style backreferences. (see L below). 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 to disambiguate: =end original 大かっこ文字クラスの外側での C<\000> 形式での 8 進エスケープは潜在的に 古い形式の後方参照を破壊します。 (後述の L 参照)。 これらはどちらも逆スラッシュに引き続く数値で構成されます。 それで 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 (in decimal), and Perl already has seen N capture groups, Perl considers this a backreference. Otherwise, it considers it an octal escape. If N has more than three digits, Perl takes only the first three for the octal escape; the rest are matched as is. =end original 逆スラッシュに引き続く数値が N (10 進) で、Perl がすでに N 個の 捕捉グループを見ているなら、Perl はこれを後方参照として扱います。 さもなければ、これは 8 進エスケープとして扱います。 N が 3 桁より長いなら、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 =begin original You can force a backreference interpretation always by using the C<\g{...}> form. You can the force an octal interpretation always by using the C<\o{...}> form, or for numbers up through \077 (= 63 decimal), by using three digits, beginning with a "0". =end original 常に C<\g{...}> 形式を使うことで後方参照に解釈するように強制できます。 常に C<\o{...}> 形式を使うか、\077 (= 10 進で 63) までの数値の場合は、 "0" を先頭に付けて常に 3 桁で使うことで 8 進数に解釈するように強制できます。 =head3 Hexadecimal escapes (16 進数エスケープ) =begin original Like octal escapes, there are two forms of hexadecimal escapes, but both start with the same thing, C<\x>. This is followed by either exactly two hexadecimal digits forming a 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 8 進エスケープと同様、16 進エスケープにも二つの形式がありますが、どちらも 同じ C<\x> で始まります。 これに正確に 2 桁の数値を形成する 16 進数、あるいは中かっこで囲まれた 任意の桁の 16 進数が引き続きます。 16 進数は表現したい文字の符号位置です。 =begin original Note that a character expressed as one of these escapes is considered a character without special meaning by the regex engine, and will match "as is". =end original これらのエスケープの一つでで表現される文字は正規表現エンジンにとって特別な 意味を持たない文字として考えられ、「そのまま」でマッチングすることに 注意してください。 =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 provide functionality similar to 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>, whichever comes first. They provide functionality similar to what the functions C and C provide. =end original 複数の文字を大文字または小文字にするのに、C<\L> または C<\U> を 使いたいかもしれません; これは、パターンの末尾または次に C<\E> が現れるまでの全ての文字を小文字/大文字にします。 これらは関数 C および C と同じような機能を提供します。 =begin original C<\Q> is used to quote (disable) pattern metacharacters, up to the next C<\E> or the end of the pattern. C<\Q> adds a backslash to any character that could have special meaning to Perl. In the ASCII range, it quotes every character that isn't a letter, digit, or underscore. See L for details on what gets quoted for non-ASCII code points. Using this ensures that any character between C<\Q> and C<\E> will be matched literally, not interpreted as a metacharacter by the regex engine. =end original C<\Q> は引き続く文字列を、次の C<\E> またはパターンの最後まで クォート(無効化)します。 C<\Q> は Perl で特殊な意味を持つ全ての文字に逆スラッシュを追加します。 ASCII の範囲では、英字、数字、下線でないものをクォートします。 非 ASCII 符号位置で何がクォートされるかに関する詳細については L を参照してください。 これにより C<\Q> と C<\E> の間の全ての文字はリテラルにマッチングし、 正規表現エンジンによってメタ文字として解釈されません。 =begin original C<\F> can be used to casefold all characters following, up to the next C<\E> or the end of the pattern. It provides the functionality similar to the C function. =end original C<\F> は次の C<\E> またはパターンの最後まで畳み込み文字にします。 これは C 関数と似たような機能を提供します。 =begin original Mnemonic: Iowercase, Ippercase, Iold-case, Iuotemeta, Ind. =end original 記憶法: 小文字(Iowercase)、大文字(Ippercase)、畳み込み文字 (Iold-case)、クォート(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, Unicode marks, and connector punctuation (like the 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<単語> 文字 (英字、数字、Unicode マーク、 (下線のような)接続句読点) にマッチングする文字クラスです。 C<\d> は 10 進数字にマッチングする文字クラスです; 一方文字クラス C<\s> は 空白文字にマッチングします。 perl 5.10.0 から導入されたのは、それぞれ水平空白文字と垂直空白文字に マッチングする C<\h> と C<\v> です。 =begin original The exact set of characters matched by C<\d>, C<\s>, and C<\w> varies depending on various pragma and regular expression modifiers. It is possible to restrict the match to the ASCII range by using the C regular expression modifier. See L. =end original C<\d>, C<\s>, C<\w> でマッチングする文字の正確な集合は様々なプラグマと 正規表現修飾子に依存して異なります。 C 正規表現修飾子を使うことで ASCII の範囲にマッチングするように 制限できます。 L を参照してください。 =begin original The uppercase variants (C<\W>, C<\D>, C<\S>, C<\H>, and C<\V>) are character classes that match, respectively, any character that isn't a word character, digit, whitespace, horizontal whitespace, or 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 Either C<\gI> (starting in Perl 5.10.0), or C<\I> (old-style) where I is a positive (unsigned) decimal number of any length is an absolute reference to a capturing group. =end original (Perl 5.10.0 に導入された) C<\gI> または (古い形式の) C<\I> で I が 任意の長さの正の(符号なし)10 進数の場合、捕捉グループへの絶対参照です。 =begin original I refers to the Nth set of parentheses, so C<\gI> refers to whatever has been matched by that set of parentheses. Thus C<\g1> refers to the first capture group in the regex. =end original I はかっこの N 番目の集合を参照するので、C<\gI> はかっこのその集合に マッチングするものを参照します。 従って、C<\g1> は正規表現中の最初の捕捉グループを参照してます。 =begin original The C<\gI> form can be equivalently written as C<\g{I}> which avoids ambiguity when building a regex by concatenating shorter strings. Otherwise if you had a regex C, and C<$a> contained C<"\g1">, and C<$b> contained C<"37">, you would get C which is probably not what you intended. =end original C<\gI> 形式は、より短い文字列を結合することにより正規表現を構築するときの 曖昧さを避けるために C<\g{I}> と等価に書けます。 さもなければ、C という正規表現があって、C<$a> に C<"\g1"> が入り、 C<$b> に C<"37"> が入っていると、C を得ることになり、おそらく 望んでいないものでしょう。 =begin original In the C<\I> form, I must not begin with a "0", and there must be at least I capturing groups, or else I is considered an octal escape (but something like C<\18> is the same as C<\0018>; that is, the octal escape C<"\001"> followed by a literal digit C<"8">). =end original C<\I> 形式で、I は "0" で始まってはならず、少なくとも I 個の 捕捉グループがなければなりません; さもなければ I は 8 進エスケープと 考えられます (しかし C<\18> のようなものは C<\0018> と同じです; つまり、 8 進エスケープ C<"\001"> に引き続いてリテラルの数値 C<"8"> です)。 =begin original Mnemonic: Iroup. =end original 記憶法: グループ(Iroup)。 =head4 Examples (例) /(\w+) \g1/; # Finds a duplicated word, (e.g. "cat cat"). /(\w+) \1/; # Same thing; written old-style /(.)(.)\g2\g1/; # Match a four letter palindrome (e.g. "ABBA"). =head3 Relative referencing (相対参照) =begin original C<\g-I> (starting in Perl 5.10.0) is used for relative addressing. (It can be written as C<\g{-I>.) It refers to the Ith group before the C<\g{-I}>. =end original (Perl 5.10.0 に導入された) C<\g-I> は相対参照のために使われます。 (これは C<\g{-I> と書けます。) C<\g{-N}> の N 個前のグループを参照します。 =begin original The big advantage of this form 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 この形式の大きな利点は、捕捉グループを含んでいるような大きなパターンに 展開されるような参照のパターンをより簡単に書けることです。 =head4 Examples (例) /(A) # Group 1 ( # Group 2 (B) # Group 3 \g{-1} # Refers to group 3 (B) \g{-3} # Refers to group 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 C<\g{I}> (starting in Perl 5.10.0) can be used to back refer to a named capture group, dispensing completely with having to think about capture buffer positions. =end original (Perl 5.10.0 に導入された) C<\g{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 To prevent any ambiguity, I must not start with a digit nor contain a hyphen. =end original 曖昧さを避けるために、I は数値で始まったりハイフンを含んだりしては いけません。 =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 to 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 to C; that is, it matches at the end of the string, or one 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 does not match before a trailing newline. C<\z> matches at the end of the string only, regardless of the modifiers used, and not just before a newline. It is how to anchor the match to the true end of the string under all conditions. =end original C<\z> は C<\Z> と同様ですが、末尾の改行の手前ではマッチングしません。 C<\z> は文字列の末尾にのみマッチングします - 修飾子が使われるかや改行の 前かに関わりません。 これはアンカーがあらゆる条件の基で文字列の真の末尾にマッチングする方法です。 =item \G =begin original C<\G> is usually used only in combination with the C modifier. If the C modifier is used and the match is done in scalar context, Perl remembers 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 on that string ended, or the beginning of that 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 Do not use something like C<\b=head\d\b> and expect it to match the beginning of a line. It can't, because for there to be a boundary before the non-word "=", there must be a word character immediately previous. All boundary determinations look for word characters alone, not for non-words characters nor for string ends. It may help to understand how <\b> and <\B> work by equating them as follows: =end original C<\b=head\d\b> のようなものを使って行頭にマッチングすると 仮定しないでください。 これはできません; なぜなら非単語 "=" の前に境界があり、直前に単語文字が なければならないからです。 全ての境界決定は単語文字のみを探し、非単語文字や文字列の末尾は探しません。 <\b> と <\B> がどのように動作するかは以下のように同等化することで 理解の助けになるでしょう: \b really means (?:(?<=\w)(?!\w)|(?oundary. =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. These 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. This is very dangerous, because it violates the logical character abstraction and can cause UTF-8 sequences to become malformed. =end original C<\C> は、例えソース文字列が UTF-8 形式でエンコードされていても、 常に単一のオクテットにマッチングし、マッチングする文字は複数オクテット 文字です。 C<\C> は perl 5.6 で導入されました。 これはとても危険です; なぜならこれは論理文字抽象化に違反し、UTF-8 並びを 不正なものにする可能性があるからです。 =begin original Mnemonic: oItet. =end original 記憶法: オクテット(oItet)。 =item \K =begin original This appeared in perl 5.10.0. Anything matched left of C<\K> is not included in C<$&>, and will not be replaced if the pattern is used in a substitution. This lets you 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 an experimental feature new to perl 5.12.0. It matches any character that is B 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 からの実験的な機能です。 これは改行 B<でない> 任意の文字にマッチングします。 これは 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 and character sequences> を 意味するかも知れないことに注意してください。 =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 considered a linebreak sequence 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, sometimes called the network newline; it's the end of line sequence used in Microsoft text files opened in binary mode). C<\R> is equivalent to C<< (?>\x0D\x0A|\v) >>. (The reason it doesn't backtrack is that the sequence is considered inseparable. That means that =end original C<\R> は I<一般的な改行> にマッチングします; これは Unicode で改行並びとして 扱われるものです。 これには C<\v> (垂直空白) でマッチングする全ての文字、複数文字並び C<"\x0D\x0A"> (復帰に引き続いて改行、時々ネットワーク改行と呼ばれるもの; これは Microsoft テキストファイルをバイナリモードで開いた時に使われる 行末並びです) を含みます。 C<\R> は C<< (?>\x0D\x0A)|\v) >> と等価です。 (バックトラックしない理由は、並びは分割できないと考えられるからです。 つまり、 "\x0D\x0A" =~ /^\R\x0A$/ # No match =begin original fails, because the C<\R> matches the entire string, and won't backtrack to match just the C<"\x0D">.) 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> は文字列全体にマッチングし、C<"\x0D"> だけを マッチングするようにバックトラックしないからです。) C<\R> は複数の文字の並びにマッチングするので、大かっこ文字クラスの内側には 使えません; C はエラーです; 代わりに C<\v> を使ってください。 C<\R> は perl 5.10.0 で導入されました。 =begin original Note that this does not respect any locale that might be in effect; it matches according to the platform's native character set. =end original これは有効かもしれないロケールを認識しないことに注意してください; これは プラットフォームのネイティブな文字集合に従ってマッチングします。 =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 its 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 (0x256) takes 2 octets in UTF-8. $str =~ s/foo\Kbar/baz/g; # Change any 'bar' following a 'foo' to 'baz' $str =~ s/(.)\K\g1//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{307}" =~ /^\X$/ # \X matches a P with a dot above. =cut =begin meta Translate: SHIRAKATA Kentaro (5.10.1-) Status: completed =end meta