5.38.0

名前

perlrebackslash - Perl Regular Expression Backslash Sequences and Escapes

perlrebackslash - Perl 正規表現逆スラッシュシーケンスとエスケープ

説明

The top level documentation about Perl regular expressions is found in perlre.

Perl 正規表現に関する最上位文書は perlre です。

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.

この文書は全ての逆スラッシュシーケンスとエスケープシーケンスについて 記述します。 逆スラッシュの役割について説明したあと、Perl 正規表現の中で特別な意味を持つ 全てのシーケンスを(アルファベット順に)示し、それぞれについて記述します。

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.

ほとんどのシーケンスは他の文書に詳細に記述されています; この文書の主な目的は 全ての逆スラッシュシーケンスとエスケープシーケンスを記述した クイックリファレンスガイドとなることです。

逆スラッシュ

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, \| matches a vertical bar, it's not an alternation), or it is the start of a backslash or escape sequence.

正規表現において、逆スラッシュは二つのタスクのどちらかを行います: 引き続く文字の特殊な意味を取り除く(例えば、(\| は代替ではなく、垂直バーに マッチングします)か、逆スラッシュシーケンスまたはエスケープシーケンスの 開始となります。

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.

これが何かを決定する規則はかなり単純です: 逆スラッシュに引き続く文字が ASCII 句読点(非単語)文字(これは英字、数字、下線以外の全ての文字)なら、 逆スラッシュは引き続く文字の特別な意味を取り除きます。

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].

逆スラッシュに引き続く文字が ASCII 英字または ASCII 数字なら、この並びは 特別です; その場合、以下に示します。 いくつかの文字はまだ使われておらず、これを逆スラッシュでエスケープするのは 特別なものにはしません。 将来のバージョンの Perl はこれに特別な意味を割り当てるかもしれないので、 しかし、警告が有効なら、そのような並びを使うと Perl は警告を出力します。 [1]。

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.

しかし、逆スラッシュシーケンスやエスケープシーケンスが逆スラッシュの後に 句読点文字がくることがないことは今だけではなく将来のバージョンの Perl 5 でも保証されています。 従って、非単語文字の前に逆スラッシュを置くのは安全です。

Note that the backslash itself is special; if you want to match a backslash, you have to escape the backslash with a backslash: /\\/ matches a single backslash.

逆スラッシュ自体が特別であることに注意してください; 逆スラッシュと マッチングしたいなら、逆スラッシュを逆スラッシュでエスケープする必要が あります: /\\/ は単一の逆スラッシュにマッチングします。

[1]

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 "Gory details of parsing quoted constructs" in perlop.

一つの例外があります。 パターンの区切りとして英数字を使っている(おそらくこれは可読性の理由で するべきではありません)場合、その文字にマッチングしたいなら区切り文字を エスケープする必要があります。 Perl は警告を出しません。 "Gory details of parsing quoted constructs" in perlop も参照してください。

全てのシーケンスとエスケープ

Those not usable within a bracketed character class (like [\da-z]) are marked as Not in [].

([\da-z] のような) 大かっこ文字クラスで使えないものは [] 内では使えない。 と記しています。

 \000              Octal escape sequence.  See also \o{}.
 \1                Absolute backreference.  Not in [].
 \a                Alarm or bell.
 \A                Beginning of string.  Not in [].
 \b{}, \b          Boundary. (\b is a backspace in []).
 \B{}, \B          Not a boundary.  Not in [].
 \cX               Control-X.
 \d                Match any digit character.
 \D                Match any character that isn't a digit.
 \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                Match any horizontal whitespace character.
 \H                Match any character that isn't 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                Match any character but newline.  Not in [].
 \N{}              Named or numbered (Unicode) character or sequence.
 \o{}              Octal escape sequence.
 \p{}, \pP         Match any character with the given Unicode property.
 \P{}, \PP         Match any character without the given property.
 \Q                Quote (disable) pattern metacharacters till \E.  Not
                   in [].
 \r                Return character.
 \R                Generic new line.  Not in [].
 \s                Match any whitespace character.
 \S                Match any character that isn't a whitespace.
 \t                Tab character.
 \u                Titlecase next character.  Not in [].
 \U                Uppercase till \E.  Not in [].
 \v                Match any vertical whitespace character.
 \V                Match any character that isn't vertical whitespace
 \w                Match any word character.
 \W                Match any character that isn't a word character.
 \x{}, \x00        Hexadecimal escape sequence.
 \X                Unicode "extended grapheme cluster".  Not in [].
 \z                End of string.  Not in [].
 \Z                End of string.  Not in [].
 \000              8 進数エスケープシーケンス。\o{} も参照。
 \1                絶対後方参照。[] 内では使えない。
 \a                アラームまたはベル。
 \A                文字列の先頭。[] 内では使えない。
 \b{}, \b          境界。(\b は [] 内では後退)。
 \B{}, \B          境界でない。[] 内では使えない。
 \cX               Control-X。
 \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                文字列の末尾。[] 内では使えない。

文字エスケープ

固定文字

A handful of characters have a dedicated character escape. 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 "OPERATOR DIFFERENCES" in perlebcdic.)

一部の文字列は 文字エスケープ として動作します。 これらは、ASCII 符号位置 (10 進と 16 進)、ASCII の名前、ASCII プラットフォームでの制御エスケープ、短い説明と共に、以下の表に示しています。 (EBCDIC プラットフォームについては、"OPERATOR DIFFERENCES" in perlebcdic を 参照してください。)

 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
 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    タブ
[1]

\b is the backspace character only inside a character class. Outside a character class, \b alone is a word-character/non-word-character boundary, and \b{} is some other type of boundary.

\b は文字クラスの中でのみ復帰文字です。 文字クラスの外側では、\b 単独はは単語文字/非単語文字境界で、 \b{} は他の種類の境界です。

[2]

\n matches a logical newline. Perl converts between \n and your OS's native newline character when reading from or writing to text files.

\n は論理改行にマッチングします。 Perl はテキストファイルの読み書きをする時には \n OS ネイティブな 改行文字との間を変換します。

 $str =~ /\t/;   # Matches if $str contains a (horizontal) tab.

制御文字

\c is used to denote a control character; the character following \c determines the value of the construct. For example the value of \cA is chr(1), and the value of \cb is chr(2), etc. The gory details are in "Regexp Quote-Like Operators" in perlop. A complete list of what chr(1), etc. means for ASCII and EBCDIC platforms is in "OPERATOR DIFFERENCES" in perlebcdic.

\c は制御文字を示すために使われます; \c に引き続く文字は 構文の値を決定します。 例えば \cA の値は chr(1)\cb の値は chr(2)、などです。 内部の詳細は "Regexp Quote-Like Operators" in perlop にあります。 ASCII と EBCDIC プラットフォームで chr(1) などが意味するものの完全な 一覧は "OPERATOR DIFFERENCES" in perlebcdic にあります。

Note that \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\X means chr(28) . 'X' for all characters X.

正規表現 (またはダブルクォートされた文字列) の末尾に単体で \c\ を置くのは 不正です。 逆スラッシュには他の文字が引き続かなければなりません。 つまり、全ての文字 X に対して \c\Xchr(28) . 'X' を意味する ということです。

To write platform-independent code, you must use \N{NAME} instead, like \N{ESCAPE} or \N{U+001B}, see charnames.

プラットフォーム独立なコードを書くためには、代わりに \N{ESCAPE}\N{U+001B} のような \N{NAME} を使わなければなりません; charnames を参照してください。

Mnemonic: control character.

記憶法: 制御文字(control character)。

 $str =~ /\cK/;  # Matches if $str contains a vertical tab (control-K).

名前付きまたは番号による文字および文字並び

Unicode characters have a Unicode name and numeric code point (ordinal) value. Use the \N{} construct to specify a character by either of these values. Certain sequences of characters also have names.

Unicode 文字は Unicode 名と数値の符号位置(序数)を持ちます。 文字をそれらの値で指定するには \N{} 構文を使います。 一部の文字並びにも名前があります。

To specify by name, the name of the character or character sequence goes between the curly braces.

名前を指定するには、文字や文字並びの名前は中かっこの間に書きます。

To specify a character by Unicode code point, use the form \N{U+code point}, where code point 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 \N{U+0041} means LATIN CAPITAL LETTER A, and you will rarely see it written without the two leading zeros. \N{U+0041} means "A" even on EBCDIC machines (where the ordinal value of "A" is not 0x41).

文字を Unicode 符号位置で指定するには、\N{U+code point} 形式を使います; ここで code point は Unicode が目的の文字に割り当てている符号位置を 16 進数で指定します。 数値を 4 桁にパッディングするために先頭にゼロを付ける慣習がありますが、 必須ではありません。 従って \N{U+0041}LATIN CAPITAL LETTER A を意味し、先頭の二つの ゼロなしで書かれるのを見ることは稀でしょう。 \N{U+0041} は ("A" の序数が 0x41 でない) EBCDIC マシンでも "A" を 意味します。

Blanks may freely be inserted adjacent to but within the braces enclosing the name or code point. So \N{ U+0041 } is perfectly legal.

名前や符号位置を囲む中かっこの内側隣接する部分には空白を自由に 挿入できます。 従って \N{ U+0041 } は完全に正当です。

It is even possible to give your own names to characters and character sequences by using the charnames module. These custom names are lexically scoped, and so a given code point may have different names in different scopes. The name used is what is in effect at the time the \N{} is expanded. For patterns in double-quotish context, that means at the time the pattern is parsed. But for patterns that are delimitted by single quotes, the expansion is deferred until pattern compilation time, which may very well have a different charnames translator in effect.

charnames モジュールを使って 文字や文字並びに独自の名前をつけることすら可能です。 これらのカスタム名はレキシカルスコープなので、 指定された符号位置は、異なるスコープでは異なる名前かも知れません。 使われる名前は、\N{} が展開された時点で有効なものです。 ダブルクォート風コンテキストのパターンでは、 これはパターンがパースされた時点を意味します。 しかし、シングルクォートで区切られたパターンの場合、 展開はパターンのコンパイル島で遅延されるので、 異なった charnames 変換器を持っているかもしれません。

(There is an expanded internal form that you may see in debug output: \N{U+code point.code point...}. The ... means any number of these code points 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.)

(デバッグ出力で見るかも知れない拡張内部形式 \N{U+code point.code point...} があります。 ... は任意の数の code point をドットで区切ったものです。 これは文字によって形成される並びを表現します。 これは内部形式専用で、変更されるかも知れず、自分自身で 使おうとするべきではありません。)

Mnemonic: Named character.

記憶法: 名前付き文字(Named character)。

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".

名前付きまたは番号による文字または文字並びで表現される文字は 正規表現エンジンにとって特別な意味を持たない文字として扱われ、 「そのまま」でマッチングすることに注意してください。

 $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".

8 進数エスケープ

There are two forms of octal escapes. Each is used to specify a character by its code point specified in base 8.

8 進数エスケープには二つの形式があります。 それぞれは 8 進数で指定された符号位置で文字を指定するために使われます。

One form, available starting in Perl 5.14 looks like \o{...}, where the dots represent one or more octal digits. It can be used for any Unicode character.

Perl 5.14 から利用可能になった一つ目の形式は \o{...} のようなもので、 ドットは一つ以上の 8 進数を表現します。 これはどのような Unicode 文字にも使えます。

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 "Disambiguation rules between old-style octal escapes and backreferences" 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.

これは全ての Perl で利用可能なもう一つの形式の潜在的な問題を避けるために 導入されました。 この形式は逆スラッシュに引き続いて 3 桁の 8 進数です。 この形式の問題の一つは、古い形式の後方参照とまったく同じように見えることです (以下の "Disambiguation rules between old-style octal escapes and backreferences" を参照してください。) 3 桁の数字の最初の文字を常にゼロにすることにこれを避けられますが、指定できる 最大の符号位置が \077 になります。

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 "Absolute referencing" for more discussion and examples of the snippet problem.

ある種のコンテキストでは、逆スラッシュに引き続いて二つや一つの 8 進数が 8 進エスケープとして解釈され、時々警告が出て、ある種のバグにより、時々 驚くような結果になります。 また、より小さいスニペットを結合して正規表現を作成して、3 桁より小さいものを 使うと、一つのスニペットの最初がその前のスニペットの終わりに追加された 数値と解釈されることがあります。 スニペット問題に関するさらなる議論と例については "Absolute referencing" を 参照してください。

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".

8 進エスケープで表現される文字は正規表現エンジンにとって特別な意味を持たない 文字として考えられ、「そのまま」でマッチングすることに注意してください。

To summarize, the \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.

まとめると、\o{} 形式は常に安全で、もう一つの形式は、指定するときに 正確に 3 桁を使う場合 \077 までの符号位置なら安全です。

Mnemonic: 0ctal or octal.

記憶法: 8 進数(0ctal または octal)。

例(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).
 /\o{ 400}/          # LATIN CAPITAL LETTER A WITH MACRON
 /\o{ 400 }/         # Same. These show blanks are allowed adjacent to
                     # the braces

古い形式の 8 進エスケープと後方参照との曖昧さをなくす規則

Octal escapes of the \000 form outside of bracketed character classes potentially clash with old-style backreferences (see "Absolute referencing" 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:

大かっこ文字クラスの外側での \000 形式での 8 進エスケープは潜在的に 古い形式の後方参照を破壊します (後述の "Absolute referencing" 参照)。 これらはどちらも逆スラッシュに引き続く数値で構成されます。 それで Perl は後方参照か 8 進エスケープかを決定するのに発見的手法を 使います。 Perl は曖昧さをなくすために以下の規則を使います:

  1. If the backslash is followed by a single digit, it's a backreference.

    逆スラッシュに引き続くのが単一の数字なら、それは後方参照です。

  2. If the first digit following the backslash is a 0, it's an octal escape.

    逆スラッシュに引き続く文字が 0 なら、それは 8 進エスケープです。

  3. 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.

    逆スラッシュに引き続く数値が 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'.

You can force a backreference interpretation always by using the \g{...} form. You can the force an octal interpretation always by using the \o{...} form, or for numbers up through \077 (= 63 decimal), by using three digits, beginning with a "0".

常に \g{...} 形式を使うことで後方参照に解釈するように強制できます。 常に \o{...} 形式を使うか、\077 (= 10 進で 63) までの数値の場合は、 "0" を先頭に付けて常に 3 桁で使うことで 8 進数に解釈するように強制できます。

16 進数エスケープ

Like octal escapes, there are two forms of hexadecimal escapes, but both start with the sequence \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.

8 進エスケープと同様、16 進エスケープにも二つの形式がありますが、どちらも 並び \x で始まります。 これに正確に 2 桁の数値を形成する 16 進数、あるいは中かっこで囲まれた 任意の桁の 16 進数が引き続きます。 16 進数は表現したい文字の符号位置です。

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".

これらのエスケープの一つでで表現される文字は正規表現エンジンにとって特別な 意味を持たない文字として考えられ、「そのまま」でマッチングすることに 注意してください。

Mnemonic: hexadecimal.

記憶法: 16 進数(hexadecimal)。

例(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.
 /\x{ 263b }/       # Same, showing optional blanks adjacent to the
                    # braces

修飾子

A number of backslash sequences have to do with changing the character, or characters following them. \l will lowercase the character following it, while \u will uppercase (or, more accurately, titlecase) the character following it. They provide functionality similar to the functions lcfirst and ucfirst.

多くの逆スラッシュシーケンスは、引き続く文字を変更します。 \l は引き続く文字を小文字に変えます; 一方\u は引き続く文字を大文字(より 正確にはタイトル文字)にします。 これは関数 lcfirst および ucfirst と似たような機能を提供します。

To uppercase or lowercase several characters, one might want to use \L or \U, which will lowercase/uppercase all characters following them, until either the end of the pattern or the next occurrence of \E, whichever comes first. They provide functionality similar to what the functions lc and uc provide.

複数の文字を大文字または小文字にするのに、\L または \U を 使いたいかもしれません; これは、パターンの末尾または次に \E が現れるまでの全ての文字を小文字/大文字にします。 これらは関数 lc および uc と同じような機能を提供します。

\Q is used to quote (disable) pattern metacharacters, up to the next \E or the end of the pattern. \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 "quotemeta" in perlfunc for details on what gets quoted for non-ASCII code points. Using this ensures that any character between \Q and \E will be matched literally, not interpreted as a metacharacter by the regex engine.

\Q は引き続く文字列を、次の \E またはパターンの最後まで クォート(無効化)します。 \Q は Perl で特殊な意味を持つ全ての文字に逆スラッシュを追加します。 ASCII の範囲では、英字、数字、下線でないものをクォートします。 非 ASCII 符号位置で何がクォートされるかに関する詳細については "quotemeta" in perlfunc を参照してください。 これにより \Q\E の間の全ての文字はリテラルにマッチングし、 正規表現エンジンによってメタ文字として解釈されません。

\F can be used to casefold all characters following, up to the next \E or the end of the pattern. It provides the functionality similar to the fc function.

\F は次の \E またはパターンの最後まで畳み込み文字にします。 これは fc 関数と似たような機能を提供します。

Mnemonic: Lowercase, Uppercase, Fold-case, Quotemeta, End.

記憶法: 小文字(Lowercase)、大文字(Uppercase)、畳み込み文字 (Fold-case)、クォート(Quotemeta)、終了(End)。

 $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\)/

文字クラス

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 perlrecharclass.

Perl の正規表現は広い範囲の文字クラスを持っています。 文字クラスの一部は逆スラッシュシーケンスとして書かれます。 ここではそれらについて簡潔に議論します; 文字クラスに関する完全な詳細は perlrecharclass にあります。

\w is a character class that matches any single word character (letters, digits, Unicode marks, and connector punctuation (like the underscore)). \d is a character class that matches any decimal digit, while the character class \s matches any whitespace character. New in perl 5.10.0 are the classes \h and \v which match horizontal and vertical whitespace characters.

\w は単一の任意の 単語 文字 (英字、数字、Unicode マーク、 (下線のような)接続句読点) にマッチングする文字クラスです。 \d は 10 進数字にマッチングする文字クラスです; 一方文字クラス \s は 空白文字にマッチングします。 perl 5.10.0 から導入されたのは、それぞれ水平空白文字と垂直空白文字に マッチングする \h\v です。

The exact set of characters matched by \d, \s, and \w varies depending on various pragma and regular expression modifiers. It is possible to restrict the match to the ASCII range by using the /a regular expression modifier. See perlrecharclass.

\d, \s, \w でマッチングする文字の正確な集合は様々なプラグマと 正規表現修飾子に依存して異なります。 /a 正規表現修飾子を使うことで ASCII の範囲にマッチングするように 制限できます。 perlrecharclass を参照してください。

The uppercase variants (\W, \D, \S, \H, and \V) are character classes that match, respectively, any character that isn't a word character, digit, whitespace, horizontal whitespace, or vertical whitespace.

大文字の変種 (\W, \D, \S, \H, \V) はそれぞれ単語文字、数字、 空白、水平空白、垂直空白ではない任意の文字にマッチングします。

Mnemonics: word, digit, space, horizontal, vertical.

記憶法: 単語(word)、数字(digit)、空白(space)、水平(horizontal)、 垂直(vertical)。

Unicode クラス

\pP (where P is a single letter) and \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 \PP and \P{Property} make the sequence match a character that doesn't match the given Unicode property. For more details, see "Backslash sequences" in perlrecharclass and "Unicode Character Properties" in perlunicode.

\pP (ここで P は単一の文字) と \p{Property} は与えられた Unicode 特性にマッチングする文字にマッチングしていました; 特性には "letter" や "thai character" のようなものが含まれます。 並びを \PP\P{Property} のようにキャピタライズすると、与えられた Unicode 特性にマッチングしない文字にマッチングするようになります。 さらなる詳細については、"Backslash sequences" in perlrecharclass"Unicode Character Properties" in perlunicode を参照してください。

Mnemonic: property.

記憶法: 特性(property)。

参照

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 backreference: absolutely, relatively, and by name.

正規表現で捕捉かっこが使われると、マッチングしたソース文字列の一部を 参照したり、全く同じものにマッチングしたりできます。 このような 後方参照 を参照するには三つの方法があります: 絶対、相対、 名前付きです。

絶対参照

Either \gN (starting in Perl 5.10.0), or \N (old-style) where N is a positive (unsigned) decimal number of any length is an absolute reference to a capturing group.

(Perl 5.10.0 に導入された) \gN または (古い形式の) \NN が 任意の長さの正の(符号なし)10 進数の場合、捕捉グループへの絶対参照です。

N refers to the Nth set of parentheses, so \gN refers to whatever has been matched by that set of parentheses. Thus \g1 refers to the first capture group in the regex.

N はかっこの N 番目の集合を参照するので、\gN はかっこのその集合に マッチングするものを参照します。 従って、\g1 は正規表現中の最初の捕捉グループを参照してます。

The \gN form can be equivalently written as \g{N} which avoids ambiguity when building a regex by concatenating shorter strings. Otherwise if you had a regex qr/$a$b/, and $a contained "\g1", and $b contained "37", you would get /\g137/ which is probably not what you intended.

\gN 形式は、より短い文字列を結合することにより正規表現を構築するときの 曖昧さを避けるために \g{N} と等価に書けます。 さもなければ、qr/$a$b/ という正規表現があって、$a"\g1" が入り、 $b"37" が入っていると、/\g137/ を得ることになり、おそらく 望んでいないものでしょう。

In the \N form, N must not begin with a "0", and there must be at least N capturing groups, or else N is considered an octal escape (but something like \18 is the same as \0018; that is, the octal escape "\001" followed by a literal digit "8").

\N 形式で、N は "0" で始まってはならず、少なくとも N 個の 捕捉グループがなければなりません; さもなければ N は 8 進エスケープと 考えられます (しかし \18 のようなものは \0018 と同じです; つまり、 8 進エスケープ "\001" に引き続いてリテラルの数値 "8" です)。

Mnemonic: group.

記憶法: グループ(group)。

 /(\w+) \g1/;    # Finds a duplicated word, (e.g. "cat cat").
 /(\w+) \1/;     # Same thing; written old-style.
 /(\w+) \g{1}/;  # Same, using the safer braced notation
 /(\w+) \g{ 1 }/;# Same, showing optional blanks adjacent to the braces
 /(.)(.)\g2\g1/; # Match a four letter palindrome (e.g. "ABBA").

相対参照

\g-N (starting in Perl 5.10.0) is used for relative addressing. (It can be written as \g{-N}.) It refers to the Nth group before the \g{-N}.

(Perl 5.10.0 に導入された) \g-N は相対参照のために使われます。 (これは \g{-N} と書けます。) \g{-N} の N 個前のグループを参照します。

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.

この形式の大きな利点は、捕捉グループを含んでいるような大きなパターンに 展開されるような参照のパターンをより簡単に書けることです。

 /(A)        # Group 1
  (          # Group 2
    (B)      # Group 3
    \g{-1}   # Refers to group 3 (B)
    \g{-3}   # Refers to group 1 (A)
    \g{ -3 } # Same, showing optional blanks adjacent to the braces
  )
 /x;         # Matches "ABBA".

 my $qr = qr /(.)(.)\g{-2}\g{-1}/;  # Matches 'abab', 'cdcd', etc.
 /$qr$qr/                           # Matches 'ababcdcd'.

名前付き参照

\g{name} (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.

(Perl 5.10.0 に導入された) \g{name} は名前付き捕捉グループへの 後方参照に使われ、捕捉バッファの位置について考える必要を完全になくします。

To be compatible with .Net regular expressions, \g{name} may also be written as \k{name}, \k<name> or \k'name'.

.Net 正規表現との互換性のために、\g{name}\k{name}, \k<name>, \k'name' とも書けます。

To prevent any ambiguity, name must not start with a digit nor contain a hyphen.

曖昧さを避けるために、name は数値で始まったりハイフンを含んだりしては いけません。

 /(?<word>\w+) \g{word}/   # Finds duplicated word, (e.g. "cat cat")
 /(?<word>\w+) \k{word}/   # Same.
 /(?<word>\w+) \g{ word }/ # Same, showing optional blanks adjacent to
                           # the braces
 /(?<word>\w+) \k{ word }/ # Same.
 /(?<word>\w+) \k<word>/   # Same.  There are no braces, so no blanks
                           # are permitted
 /(?<letter1>.)(?<letter2>.)\g{letter2}\g{letter1}/
                           # Match a four letter palindrome (e.g.
                           # "ABBA")

表明

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.

表明は真である必要がある条件です; 実際に部分文字列の一部に マッチングすることはありません。 逆スラッシュシーケンスとして書かれる六つの表明があります。

\A

\A only matches at the beginning of the string. If the /m modifier isn't used, then /\A/ is equivalent to /^/. However, if the /m modifier is used, then /^/ matches internal newlines, but the meaning of /\A/ isn't changed by the /m modifier. \A matches at the beginning of the string regardless whether the /m modifier is used.

\A は文字列の先頭にのみマッチングします。 /m 修飾子が使われていなければ、/\A//^/ と等価です。 しかし、/m 修飾子が使われると、/^/ は内部改行にマッチングしますが、 /\A/ の意味は /m 修飾子によって変わりません。 \A は、/m 修飾子が使われるかどうかに関わらず文字列の先頭に マッチングします。

\z, \Z

\z and \Z match at the end of the string. If the /m modifier isn't used, then /\Z/ is equivalent to /$/; that is, it matches at the end of the string, or one before the newline at the end of the string. If the /m modifier is used, then /$/ matches at internal newlines, but the meaning of /\Z/ isn't changed by the /m modifier. \Z matches at the end of the string (or just before a trailing newline) regardless whether the /m modifier is used.

\z\Z は文字列の末尾にマッチングします。 /m 修飾子が使われていなければ、/\Z//$/ と等価です; 文字列の 末尾か、文字列の末尾の改行の手前にマッチングします。 /m 修飾子が使われると、/$/ は内部改行にマッチングしますが、 /\Z/ の意味は /m 修飾子によって変わりません。 \Z は、/m 修飾子が使われるかどうかに関わらず文字列の末尾(あるいは 末尾の改行の直前)にマッチングします。

\z is just like \Z, except that it does not match before a trailing newline. \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.

\z\Z と同様ですが、末尾の改行の手前ではマッチングしません。 \z は文字列の末尾にのみマッチングします - 修飾子が使われるかや改行の 前かに関わりません。 これはアンカーがあらゆる条件の基で文字列の真の末尾にマッチングする方法です。

\G

\G is usually used only in combination with the /g modifier. If the /g 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.

\G は普通は /g 修飾子との組み合わせでのみ使われます。 /g 修飾子が使われ、マッチングがスカラコンテキストで行われると、 Perl は最後のマッチングが終了したソース文字列の位置を覚えていて、次回の マッチングでは、前回終わった位置からマッチングを開始します。

\G matches the point where the previous match on that string ended, or the beginning of that string if there was no previous match.

\G はその文字列で前回のマッチングが終了した位置、あるいは前回に マッチングがなければ文字列の先頭にマッチングします。

Mnemonic: Global.

記憶法: グローバル(Global)。

\b{}, \b, \B{}, \B

\b{...}, available starting in v5.22, matches a boundary (between two characters, or before the first character of the string, or after the final character of the string) based on the Unicode rules for the boundary type specified inside the braces. The boundary types are given a few paragraphs below. \B{...} matches at any place between characters where \b{...} of the same type doesn't match.

v5.22 から利用可能である \b{...} は、 中かっこの中で指定された境界タイプに関する Unicode の規則を基にして、 境界 (二つの文字の間、 文字列の最初の文字の前、文字列の最後の文字の後のいずれか) にマッチングします。 境界タイプは数段落後に述べます。 \B{...} は、同じ種類の \b{...} がマッチングしない全ての文字間に マッチングします。

\b when not immediately followed by a "{" is available in all Perls. It matches at any place between a word (something matched by \w) and a non-word character (\W); \B when not immediately followed by a "{" matches at any place between characters where \b doesn't match. To get better word matching of natural language text, see "\b{wb}" below.

\b の直後に "{" が引き続かない場合は、全ての Perl で利用可能です。 これは単語文字 (\w でマッチングするもの) と非単語文字 (\W) の境界に マッチングします; \B の直後に "{" が引き続かない場合は、\b が マッチングしない任意の文字間にマッチングします。 よりよい自然言語テキストの単語マッチングのためには、 後述の "\b{wb}" を参照してください。

\b and \B assume there's a non-word character before the beginning and after the end of the source string; so \b will match at the beginning (or end) of the source string if the source string begins (or ends) with a word character. Otherwise, \B will match.

\b\B は、ソース文字列の先頭の前と末尾の後ろに非単語文字があると 仮定します; 従って \b は、ソース文字列が単語文字で始まっている(または 終わっている)場合はソース文字列の先頭(または末尾)にマッチングします。 さもなければ、\B がマッチングします。

Do not use something like \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 plain \b and \B boundary determinations look for word characters alone, not for non-word characters nor for string ends. It may help to understand how \b and \B work by equating them as follows:

\b=head\d\b のようなものを使って行頭にマッチングすると 仮定しないでください。 これはできません; なぜなら非単語 "=" の前に境界があり、直前に単語文字が なければならないからです。 全ての単なる \b および \B の境界決定は単語文字のみを探し、非単語文字や 文字列の末尾は探しません。 \b\B がどのように動作するかは以下のように同等化することで 理解の助けになるでしょう:

    \b  really means    (?:(?<=\w)(?!\w)|(?<!\w)(?=\w))
    \B  really means    (?:(?<=\w)(?=\w)|(?<!\w)(?!\w))

In contrast, \b{...} and \B{...} may or may not match at the beginning and end of the line, depending on the boundary type. These implement the Unicode default boundaries, specified in https://www.unicode.org/reports/tr14/ and https://www.unicode.org/reports/tr29/. The boundary types are:

一方、\b{...}\B{...} は、境界の種類に依存して、 行の先頭と末尾にマッチングしたりしなかったりします。 これらは https://www.unicode.org/reports/tr14/https://www.unicode.org/reports/tr29/ で定義されている Unicode のデフォルト境界を実装しています。 境界タイプは:

\b{gcb} or \b{g}

This matches a Unicode "Grapheme Cluster Boundary". (Actually Perl always uses the improved "extended" grapheme cluster"). These are explained below under "\X". In fact, \X is another way to get the same functionality. It is equivalent to /.+?\b{gcb}/. Use whichever is most convenient for your situation.

これは Unicode の「書記素境界」("Grapheme Cluster Boundary") に マッチングします。 (実際には Perl は改良された「拡張書記素」("extended grapheme cluster") を 常に使います。) これらは "\X" で後述します。 実際のところ、\X は同じ機能を使うためのもう一つの方法です。 これは /.+?\b{gcb}/ と等価です。 状況に応じて使いやすい方を使ってください。

\b{lb}

This matches according to the default Unicode Line Breaking Algorithm (https://www.unicode.org/reports/tr14/), as customized in that document (Example 7 of revision 35) for better handling of numeric expressions.

これはデフォルトの Unicode Line Breaking Algorithm (https://www.unicode.org/reports/tr14/) に従ってマッチングしますが、 数値表現をより良く扱うために、 (Example 7 of revision 35) 文書でカスタマイズされています。

This is suitable for many purposes, but the Unicode::LineBreak module is available on CPAN that provides many more features, including customization.

これは多くの用途に使えますが、CPAN で利用可能な Unicode::LineBreak モジュールは、 カスタマイズを含むより多くの機能が利用可能です。

\b{sb}

This matches a Unicode "Sentence Boundary". This is an aid to parsing natural language sentences. It gives good, but imperfect results. For example, it thinks that "Mr. Smith" is two sentences. More details are at https://www.unicode.org/reports/tr29/. Note also that it thinks that anything matching "\R" (except form feed and vertical tab) is a sentence boundary. \b{sb} works with text designed for word-processors which wrap lines automatically for display, but hard-coded line boundaries are considered to be essentially the ends of text blocks (paragraphs really), and hence the ends of sentences. \b{sb} doesn't do well with text containing embedded newlines, like the source text of the document you are reading. Such text needs to be preprocessed to get rid of the line separators before looking for sentence boundaries. Some people view this as a bug in the Unicode standard, and this behavior is quite subject to change in future Perl versions.

これは Unicode の「文境界」("Sentence Boundary") にマッチングします。 これは自然言語文をパースする助けとなります。 この結果はよいけれども完璧ではないものです。 例えば、これは "Mr. Smith" を二つの文と考えます。 さらなる詳細は https://www.unicode.org/reports/tr29/ にあります。 また、(フォームフィードと垂直タブを除く) "\R" にマッチングするものも 行境界と考えることに注意してください。 \b{sb} は、表示用に自動的に行をラッピングするワードプロセッサ用に デザインされたテキストに対して動作しますが、 ハードコーディングされた行境界は基本的にテキストブロック(実際には段落)の 終わりであり、従って分の終わりと考えられます。 \b{sb} は、あなたが今読んでいる文書のソーステキストのような、 組み込みの改行を含むテキストに対しては上手く動作しません。 このようなテキストについては、文境界を探す前に行セパレータを取り除く 前処理が必要です。 これは Unicode 標準のバグであると考える人々もいて、 この振る舞いは将来の Perl バージョンで変更されるかもしれません。

\b{wb}

This matches a Unicode "Word Boundary", but tailored to Perl expectations. This gives better (though not perfect) results for natural language processing than plain \b (without braces) does. For example, it understands that apostrophes can be in the middle of words and that parentheses aren't (see the examples below). More details are at https://www.unicode.org/reports/tr29/.

これは Unicode の「単語境界」("Word Boundary") にマッチングしますが、 Perl に合わせて修正しています。 これは自然言語処理において単なる \b (中かっこなし) よりも良い (しかし完璧ではない)結果を返します。 例えば、アポストロフィは単語の途中にあってもよいけれども かっこはあってはならないということを理解します (後述する例を参照してください)。 さらなる詳細は https://www.unicode.org/reports/tr29/ にあります。

The current Unicode definition of a Word Boundary matches between every white space character. Perl tailors this, starting in version 5.24, to generally not break up spans of white space, just as plain \b has always functioned. This allows \b{wb} to be a drop-in replacement for \b, but with generally better results for natural language processing. (The exception to this tailoring is when a span of white space is immediately followed by something like U+0303, COMBINING TILDE. If the final space character in the span is a horizontal white space, it is broken out so that it attaches instead to the combining character. To be precise, if a span of white space that ends in a horizontal space has the character immediately following it have any of the Word Boundary property values "Extend", "Format" or "ZWJ", the boundary between the final horizontal space character and the rest of the span matches \b{wb}. In all other cases the boundary between two white space characters matches \B{wb}.)

単語境界について、現在の Unicode の定義としては全ての空白文字の間に マッチングします。 Perl は 5.24 からこれを調整し、単なる \b が常に機能するように、 一般的に空白の範囲を分割しないようにします。 これにより \b{wb}\b の置き換えとして使えるだけでなく、 自然言語処理では一般的により良い結果になります。 (この調整の例外は、 ある範囲の空白の直後に U+0303, COMBINING TILDE のようなものが 引き続いている場合です。 その範囲の最後の空白文字が垂直空白文字の場合、結合文字に付くように 分割されます。 正確に言うと、垂直スペースで終わる空白の範囲の直後に Word Boundary 特性の値が "Extend", "Format", "ZWJ" の文字が引き続く場合、 最後の垂直スペース文字とその範囲の残りの間の境界は \b{wb} に マッチングします。 それ以外の場合は、二つの空白文字の間の境界は\B{wb} にマッチングします。)

It is important to realize when you use these Unicode boundaries, that you are taking a risk that a future version of Perl which contains a later version of the Unicode Standard will not work precisely the same way as it did when your code was written. These rules are not considered stable and have been somewhat more subject to change than the rest of the Standard. Unicode reserves the right to change them at will, and Perl reserves the right to update its implementation to Unicode's new rules. In the past, some changes have been because new characters have been added to the Standard which have different characteristics than all previous characters, so new rules are formulated for handling them. These should not cause any backward compatibility issues. But some changes have changed the treatment of existing characters because the Unicode Technical Committee has decided that the change is warranted for whatever reason. This could be to fix a bug, or because they think better results are obtained with the new rule.

これらの Unicode 境界を使う場合、 後のバージョンの Unicode 標準を含む将来のバージョンの Perl では、 コードを書いた時点と正確に同じように動作しないかもしれないという リスクを取ることになるということを認識することは重要です。 これらの規則は安定しているとは考えられておらず、標準の他の部分よりも 変更される可能性がより高いです。 Unicode は自分の意思でこれらを変更する権利を保持していて、 Perl はその実装を Unicode の新しい規則に更新する権利を保持しています。 過去には、以前の全ての文字と異なる性格を持つ新しい文字を標準に追加するために、 変更が行われたことがあります; それらを扱うために新しい規則が策定されました。 これらは後方互換性問題は発生させないはずです。 しかし、Unicode Technical Committee が何らかの理由で変更が正当化されると 考えたことによって、既存の文字の扱いが変更されたことがあります。 これはバグを修正するためかもしれませんし、彼らが新しい規則によって より良い結果を得られると考えるからかもしれません。

It is also important to realize that these are default boundary definitions, and that implementations may wish to tailor the results for particular purposes and locales. For example, some languages, such as Japanese and Thai, require dictionary lookup to accurately determine word boundaries.

また、これらはデフォルトの境界定義であり、実装は特定の目的やロケールのために 結果を調整する必要があることがあるということを認識することが重要です。 例えば、日本語やタイ語のような一部の言語では、単語境界の正確な決定には 辞書検索が必要です。

Mnemonic: boundary.

記憶法: 境界(boundary)。

  "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'
  }

  my $s = "He said, \"Is pi 3.14? (I'm not sure).\"";
  print join("|", $s =~ m/ ( .+? \b     ) /xg), "\n";
  print join("|", $s =~ m/ ( .+? \b{wb} ) /xg), "\n";
 prints
  He| |said|, "|Is| |pi| |3|.|14|? (|I|'|m| |not| |sure
  He| |said|,| |"|Is| |pi| |3.14|?| |(|I'm| |not| |sure|)|.|"

その他

Here we document the backslash sequences that don't fall in one of the categories above. These are:

ここでは上述のカテゴリの一つに当てはまらない逆スラッシュシーケンスについて 記述します。 それは:

\K

This appeared in perl 5.10.0. Anything matched left of \K is not included in $&, and will not be replaced if the pattern is used in a substitution. This lets you write s/PAT1 \K PAT2/REPL/x instead of s/(PAT1) PAT2/${1}REPL/x or s/(?<=PAT1) PAT2/REPL/x.

これは perl 5.10.0 から現れました。 \K の左側でマッチングしたものは $& に含まれまず、 パターンが置換で使われた場合は置き換えられません。 これにより、s/(PAT1) PAT2/${1}REPL/x or s/(?<=PAT1) PAT2/REPL/x の 代わりに s/PAT1 \K PAT2/REPL/x と書けます。

Mnemonic: Keep.

Mnemonic: 保持(Keep)。

\N

This feature, available starting in v5.12, matches any character that is not a newline. It is a short-hand for writing [^\n], and is identical to the . metasymbol, except under the /s flag, which changes the meaning of ., but not \N.

v5.12 から利用可能になったこの機能は、改行 でない 任意の文字に マッチングします。 これは [^\n] の省略形で、. メタ文字と同じですが、. の意味を変える /s フラグが付いていても \N の意味は変わりません。

Note that \N{...} can mean a named or numbered character .

\N{...}名前または番号の文字 を 意味するかも知れないことに注意してください。

Mnemonic: Complement of \n.

記憶法: \n の補数。

\R

\R matches a generic newline; that is, anything considered a linebreak sequence by Unicode. This includes all characters matched by \v (vertical whitespace), and the multi character sequence "\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). \R is equivalent to (?>\x0D\x0A|\v). (The reason it doesn't backtrack is that the sequence is considered inseparable. That means that

\R一般的な改行 にマッチングします; これは Unicode で改行並びとして 扱われるものです。 これには \v (垂直空白) でマッチングする全ての文字、複数文字並び "\x0D\x0A" (復帰に引き続いて改行、時々ネットワーク改行と呼ばれるもの; これは Microsoft テキストファイルをバイナリモードで開いた時に使われる 行末並びです) を含みます。 \R(?>\x0D\x0A)|\v) と等価です。 (バックトラックしない理由は、並びは分割できないと考えられるからです。 つまり、

 "\x0D\x0A" =~ /^\R\x0A$/   # No match

fails, because the \R matches the entire string, and won't backtrack to match just the "\x0D".) Since \R can match a sequence of more than one character, it cannot be put inside a bracketed character class; /[\R]/ is an error; use \v instead. \R was introduced in perl 5.10.0.

は失敗します; \R は文字列全体にマッチングし、"\x0D" だけを マッチングするようにバックトラックしないからです。) \R は複数の文字の並びにマッチングするので、大かっこ文字クラスの内側には 使えません; /[\R]/ はエラーです; 代わりに \v を使ってください。 \R は perl 5.10.0 で導入されました。

Note that this does not respect any locale that might be in effect; it matches according to the platform's native character set.

これは有効かもしれないロケールを認識しないことに注意してください; これは プラットフォームのネイティブな文字集合に従ってマッチングします。

Mnemonic: none really. \R was picked because PCRE already uses \R, and more importantly because Unicode recommends such a regular expression metacharacter, and suggests \R as its notation.

記憶法: 実際のところありません。 \R が選ばれたのは、PCRE が既に \R を使っていて、さらに重要なのは Unicode がこのような正規表現メタ文字を勧めていて、記法として \R を 提案しているからです。

\X

This matches a Unicode extended grapheme cluster.

これは Unicode の 拡張書記素クラスタ (extended grapheme cluster) に マッチングします。

\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.

\X は普通の (非 Unicode プログラマの) 使い方で単一の文字と考えられるものに かなりうまくマッチングします。 例えば、矢印のような、何らかのダイアクリティカルマーク付きの G を考えます。 Unicode にはそのような単一の文字はありませんが、G に引き続いて Unicode の "COMBINING UPWARDS ARROW BELOW" を使うことで合成でき、Unicode を認識する ソフトウェアに依って単一の文字であるかのように表示されます。

The match is greedy and non-backtracking, so that the cluster is never broken up into smaller components.

マッチングは貪欲でバックトラッキングしないので、クラスタは決してより 小さい要素に分解されません。

See also \b{gcb}.

\b{gcb} も参照してください。

Mnemonic: eXtended Unicode character.

記憶法: 拡張 Unicode 文字(eXtended Unicode character)。

 $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.